如何在nodejs中获得ReferenceError的详细信息
ReferenceError发生在我们使用未定义的variables时,这可能会非常容易发生。 在这种情况下,我的terminal出现如下所示:
ReferenceError:未定义usrname
有什么办法可以得到这个的详细信息,如文件和linenumber发生的地方?
谢谢
当你在nodejs中有referenceError的时候你得到堆栈跟踪。
ReferenceError: d is not defined at Object.<anonymous> (E:\test.js:5:13) at Module._compile (module.js:435:26) at Object.Module._extensions..js (module.js:442:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12) at Function.Module.runMain (module.js:467:10) at startup (node.js:134:18) at node.js:961:3
上面的堆栈跟踪清楚地提到了test.js:5:13
,它们分别是文件名,行号和列号。
如果您可以捕获引发的ReferenceError
,那么您可以检查它的stack
属性,它将具有文件,行和列信息:
try { // suspect code } catch (e) { console.log(e.stack); }
你也可以尝试处理uncaughtException
:
process.on('uncaughtException', function (e) { console.log(e.stack); process.exit(1); });