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?

Community
  • 1
  • 1
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • 3
    Use the [command line version of JSLint](https://www.npmjs.org/package/jslint) then. – Quentin Feb 18 '14 at 14:10
  • 1
    @DanDascalescu Than set up the validation rules to the way you want! http://www.jslint.com/lint.html#options – epascarello Feb 18 '14 at 14:47
  • @epascarello: Do you know which combination of JSLint options will only output syntax errors? – Dan Dascalescu Feb 19 '14 at 05:13
  • 4
    Here is the [meta discussion about whether this question should be re-opened](http://meta.stackexchange.com/questions/222790/request-to-reopen-so-question-on-javascript-error-detection). – George Stocker Feb 24 '14 at 16:08
  • Dan, perhaps you could rephrase your question to something like "How to show syntax errors only **with JSHint** (and/or JSLint)", then it wouldn't look so much like a question "asking to recommend or find a tool". – RandomSeed Feb 24 '14 at 16:33
  • @RandomSeed: As part of the research I've done on the question, I've looked at the options for JSLint and JSHint and it doesn't seem possible to only show errors. I even [asked the author of JSHint on GitHub](https://github.com/jshint/jshint/issues/1533) for such an option (no answer). – Dan Dascalescu Feb 24 '14 at 17:05
  • If I am remembering correctly, you can use the Google Closure Compiler to check for syntax errors. – Tim Seguine Feb 24 '14 at 19:06
  • 1
    If you know JavaScript well, you can simply edit/fork [JSLint](https://github.com/douglascrockford/JSLint/blob/master/jslint.js) 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. – ruffin Feb 25 '14 at 12:09

6 Answers6

32

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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
cweiske
  • 30,033
  • 14
  • 133
  • 194
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]+.$
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • 2
    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 Mar 25 '15 at 21:33
  • 1
    @mynameistechno, you could also add --maxerr 10000 when calling it from the command line, as the answer seems to already imply. – Cameron Hurd Feb 20 '16 at 21:50
  • 1
    jshint 2.9.4 doesn't seem to take a "--maxerr" argument - was it removed? – erjiang Nov 04 '16 at 22:33
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
idmean
  • 14,540
  • 9
  • 54
  • 83
4

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
ernesto
  • 1,771
  • 2
  • 18
  • 18
3

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

node --check filename.js

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

Ahsan
  • 3,845
  • 2
  • 36
  • 36
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;
mirza
  • 5,685
  • 10
  • 43
  • 73