Skip to content
detect and report syntax errors in source code strings
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
example working example Aug 2, 2012
test Match latest node syntax (#20) Feb 9, 2018
.npmrc Disable package-lock.json Nov 19, 2019
.travis.yml ci: add Node 10 and 12. Nov 19, 2019
LICENSE package.json and a license Aug 2, 2012
index.js Match latest node syntax (#20) Feb 9, 2018
package.json Update tap. Nov 19, 2019
readme.markdown Update URLs. Sep 30, 2018



Detect and report syntax errors in source code strings.

build status

When you type node src.js you get a friendly error report about exactly where the syntax error is. This module lets you check for syntax errors and report them in a similarly friendly format that wrapping a try/catch around Function() or vm.runInNewContext() doesn't get you.


var fs = require('fs');
var check = require('syntax-error');

var file = __dirname + '/src.js';
var src = fs.readFileSync(file);

var err = check(src, file);
if (err) {
    console.error('ERROR DETECTED' + Array(62).join('!'));

$ node check.js
ERROR DETECTED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        if (Array.isArray(x) res.push.apply(res, x);
ParseError: Unexpected identifier


var check = require('syntax-error')

var err = check(src, file, opts={})

Check the source code string src for syntax errors. Optionally you can specify a filename file that will show up in the output.

If src has a syntax error, return an error object err that can be printed or stringified.

If there are no syntax errors in src, return undefined.

Options will be passed through to acorn-node. acorn-node defaults to options that match the most recent Node versions.


Return the long string description with a source snippet and a ^ under pointing exactly where the error was detected.



short string description of the error type


line number of the error in the original source (indexing starts at 1)


column number of the error in the original source (indexing starts at 1)


With npm do:

npm install syntax-error



You can’t perform that action at this time.