You can get a stack trace that includes the line number inside the
string, but not with eval. For that you have to use the Script module.
Something like this:
var Script=process.binding('evals').Script;
var js = "var a=0;\nvarb=1;\nthrow new Error('lol');";
try {
Script.runInThisContext(js, 'eval');
console.log('no exception');
} catch (err) {
console.log(err.stack);
}
will give you:
Error: lol
at eval:3:7
at Object.<anonymous> (/home/mscdex/tmp/eval.js:5:10)
at Module._compile (node.js:472:25)
at Module._loadScriptSync (node.js:482:10)
at Module.loadSync (node.js:352:12)
at Object.runMain (node.js:535:24)
at node.js:760:10
If you want to parse out the line number, you could do:
var lineNumber = /\s+at eval:(\d+):.+\n/mi.exec(err.stack)[1];
console.log(lineNumber); // 3