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

Dan Dascalescu picture Dan Dascalescu · Feb 18, 2014 · Viewed 31.7k times · Source

JavaScript programs can be checked for errors in IDEs or using online web apps but I'm looking for a way to detect syntax errors alone.

I've tried JSLint and JSHint and looked at their options but I haven't been able to find a combination that would exclude warnings and just shows the syntax errors.

How do I check JavaScript code for syntax errors only from the command line?

Answer

cweiske picture cweiske · Jun 24, 2014

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.