Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How can I get the line number of a SyntaxError thrown by require(id) in node.js?

How can I get the line number of a SyntaxError thrown by require(id) in node.js?

Problem

I am trying to get access to the text I highlighted in red inside my nodejs program. I currently have a try/catch block around the require call, but the stack trace I dump in the catch does not contain the information I am trying to access (namely, the line Number in 'testdebug.js' where the error occurred).

The lines highlighted in red are printed by something in node's internals, apparently. How can I store that string inside of my program? Code is below.

var syntaxError = true;
try {
    debugModule = require('./testdebug.js')
    syntaxError = false;
}
catch(e) {
    console.log(e.stack);
    //there was a syntax error.
}
Problem courtesy of: mako-taco

Solution

This guy knows what's up. His module checks the code for parser errors using esprima, and gives a useful object for dissecting them.

https://github.com/substack/node-syntax-error

Solution courtesy of: mako-taco

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How can I get the line number of a SyntaxError thrown by require(id) in node.js?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×