在浏览器中的正常 Javascript 中,当您尝试使用 undefined variable 时,Google Chrome 会在控制台中显示错误。但是在 Node.js 中没有显示错误,我认为使用 undefined variable 的函数退出了。
我添加了:
process.on('uncaughtException', function(err) {
console.log( 'uncaughtException :', err );
});
但这并没有执行。
我还在调用函数中添加了 try-catch,并且没有引发任何异常。
我正在使用:“use strict”;
所以我的问题是,当访问 undefined variable 时,有什么方法可以获取/查看错误吗?
最佳答案
Node 会自动执行此操作。
// test.js
"use strict";
function badFunction() {
return iDontExist + 3;
}
badFunction();
然后:
C:\Users\Domenic\Programming>node test.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
ReferenceError: iDontExist is not defined
at badFunction (C:\Users\Domenic\Programming\test.js:4:12)
at Object.<anonymous> (C:\Users\Domenic\Programming\test.js:7:1)
at Module._compile (module.js:444:26)
at Object..js (module.js:462:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:482:10)
at EventEmitter._tickCallback (node.js:192:40)
关于javascript - 捕获node.js undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9301829/