50

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?

8
  • 3
    Use the command line version of JSLint then. Commented Feb 18, 2014 at 14:10
  • 1
    @DanDascalescu Than set up the validation rules to the way you want! jslint.com/lint.html#options Commented Feb 18, 2014 at 14:47
  • @epascarello: Do you know which combination of JSLint options will only output syntax errors? Commented Feb 19, 2014 at 5:13
  • 4
    Here is the meta discussion about whether this question should be re-opened. Commented Feb 24, 2014 at 16:08
  • 1
    If you know JavaScript well, you can simply edit/fork JSLint and make it do what you want. Not as daunting as it sounds. The code is very clean. This question has been asked before iirc; you might find your errors-only JSLint project is pretty popular. Commented Feb 25, 2014 at 12:09

6 Answers 6

33

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.

Sign up to request clarification or add additional context in comments.

2 Comments

acorn does not seen to work for me: Error: Cannot find module './_acorn.js'
Also installable in debian with apt install node-acorn
19

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

3 Comments

If there are too many warnings and errors though you end up getting a "too many errors, quitting" message. You need to set a config file option such as maxerr:10000 to avoid this, which is not ideal.
@mynameistechno, you could also add --maxerr 10000 when calling it from the command line, as the answer seems to already imply.
jshint 2.9.4 doesn't seem to take a "--maxerr" argument - was it removed?
5

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

3 Comments

How do I configure it to show only syntax errors, not warnings?
currently it's not possible to disable all warnings in jshint. You have to do it one by one in a config file :/
On Mac OSX, this can be installed using: npm install -g jshint
5

You could use node's built-in syntax checking option:

node --check filename.js

Ref: https://nodejs.org/api/cli.html#cli_c_check

Comments

3

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

Comments

0

Here is an answer for a Continuous Integration scenario.

1- Checks all the JavaScript files with exclude folder, subfolder and file features.

2- Exit if there is an error.

esvalidate --version || npm install -g esprima;
find ./ -not \( -path ./exclude_folder_1 -prune \) -not \( -path ./exclude/sub_folder -prune \) -not \( -path ./exclude/sub_folder_2 -prune \) ! -name exclude_specified_file.js  -name \*.js -exec esvalidate '{}' \; | grep -v "No syntax errors detected" && echo "Javascript Syntax error(s) detected" && exit 1;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.