Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 587
Dan Dascalescu Avatar asked Feb 18 '14 14:02

Dan Dascalescu


People also ask

What are syntax errors in JavaScript?

An exception caused by the incorrect use of a pre-defined syntax. Syntax errors are detected while compiling or parsing source code. For example, if you leave off a closing brace ( } ) when defining a JavaScript function, you trigger a syntax error.


1 Answers

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.

like image 158
cweiske Avatar answered Sep 19 '22 18:09

cweiske