How can I check JavaScript code for syntax errors ONLY from the command line?

Solution 1:

I use acorn:

$ acorn --silent tests/files/js-error.js; echo $?
Unexpected token (1:14)
1

$ acorn --silent tests/files/js-ok.js; echo $?
0

Install via: npm -g install acorn.

Solution 2:

The solution is to enable jshint's --verbose option, which shows the error or warning code (e.g. E020 for Expected '}' to match '{' or W110 for Mixed double and single quotes), then grep for errors only:

jshint --verbose test.js | grep -E E[0-9]+.$

Solution 3:

JSHint does what you want. http://www.jshint.com/

You can configure which warnings or errors to show.

An example:

$ jshint myfile.js
myfile.js: line 10, col 39, Octal literals are not allowed in strict mode.

1 error