3

I am loading a Node.js module using require. This throws a Syntax Error (an Error object).

Is there a way to get the location in the file where the error occurred?

I can see the stack (below) but I don't have any location information. I know I can use substack's node-syntax-error, but is there a way to get similar location info from the Javascript Error object?

SyntaxError: Unexpected identifier
at Module._compile (module.js:437:25)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
... // the rest is in my code

EDIT:

According to Vadim's comment below, yes that is what I am tempted to do. I see that node throws nice errors. Having a test.js file that contains only abc you get an error from node saying

ReferenceError: abc is not defined
    at Object.<anonymous> (test.js:1:63)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

Now the question is, is there a node API to do get this error (that the node module.js module uses) such that I don't have to child_process.spawn('node', ['test.js'])?

2
  • Can node-syntax-error, that you have linked to, do it? If so why do you not want to use it? Also, have you tried looking at it's source to see how it does it? It's open-source goodness you know! Commented Aug 20, 2012 at 11:17
  • node-syntax can do it if you want to add the dependency to exprima. I was wondering if I can get a SyntaxError WITH the lineNumber and fileName as on MDN's SyntaxError page. Commented Aug 20, 2012 at 12:55

4 Answers 4

3

You can simple run node with file that contains SyntaxError and see console output.

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

1 Comment

Yes, this is going in the right direction, but is there an API inside node for it? See the edited question above!
0

Error.stack

Errors have a "stack" property that stores the stack trace.

try {
    throw new Error("with some message");
}
catch(err) {
    // print error's stack trace to stder
    console.error(err.stack);
}

running this on a file called "/home/myusername/test.js"

node /home/myusername/test.js

will output the following

Error: with some message
    at Object.<anonymous> (/home/myusername/test.js:2:11)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

2 Comments

This works for me. My error was not of syntax but I was not able to get the error. My error was something like : [TypeError: Cannot read property 'replace' of undefined] and nothing more. I needed source point of this error. After hours of research I found this and works. I will put a comment to clarify this.
For SyntaxError, this appears to be empty
0

@laconbass answer works for me.

err.stack

I tried a blog framework and I got : 500 Internal Server Error. Hopefully I put a console :

 app.use(function(error, req, res, next) {
    res.status(500).render('500');
      console.log(error);
  });

and I could see the error which was not descriptive:

TypeError: Cannot read property 'replace' of undefined

After several minuts of research I found this answers, So I tried :

 app.use(function(error, req, res, next) {
    res.status(500).render('500');
      console.log(error.stack);
  });

and I could see the source point of error : (Object.exports.replace)

TypeError: Cannot read property 'replace' of undefined
    at Object.exports.replace (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/filters.js:411:15)
    at eval (eval at <anonymous> (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:498:13), <anonymous>:41:63)
    at Object.exports.each (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/utils.js:45:11)
    at eval (eval at <anonymous> (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:498:13), <anonymous>:26:10)
    at Object.eval [as tpl] (eval at <anonymous> (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:498:13), <anonymous>:85:3)
    at compiled (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:619:18)
    at /.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:559:20
    at /.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:690:9
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:380:3)

I know that this is not a syntax error, but could be works to find the location of a javascript/node error.

I am a novice in nodejs.

I hope this helps.

Comments

0

You can use the stacktracey library (note: I'm the author of this library).

It prints nice callstacks along with source code lines, and also parses the SyntaxError output (in Node higher than v4).

For example, when trying to require this file (named test_files/syntax_error.js):

// next line contains a syntax error (not a valid JavaScript)
foo->bar ()

...the pretty printed call stack for the error thrown would be:

at (syntax error)                  test_files/syntax_error.js:2  foo->bar ()
at it                              test.js:184                   try { require ('./test_files/syntax_error.js') }
at runCallback                     timers.js:781
at tryOnImmediate                  timers.js:743
at processImmediate [as _immediat  timers.js:714

...where the first line is generated from parsing the raw output from the util.inspect call in Node.

The library also provides the full API access to the contents of a parsed call stack, so you can tune the output to whatever you want.

1 Comment

Please be careful with linking to your own content on different sites, you don't want to be a spammer. You should be including the majority of the content here, and use the link only as a reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.