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

Any JavaScript parser should do, acorn mentioned by @cweise is nice because it is fast and has a --silent switch.

You could also use esvalidate from the npm esprima package: http://ariya.ofilabs.com/2012/10/javascript-validator-with-esprima.html

$ npm install -g esprima
$ esvalidate js-file-with-errors.js 
js-file-with-errors.js:1: Invalid left-hand side in assignment

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]+.$

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.


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