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.
}
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
Discussion
View additional discussion.