javascript - 捕获node.js undefined variable

标签 javascript debugging node.js

在浏览器中的正常 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/

相关文章:

javascript - 如何从新的空 MVC 项目中删除 js 脚本?

javascript - 如何在点击时显示 Angular UI Bootstrap 日期选择器?

带参数的 JavaFX JavaScript 上行调用

c - 取决于调试级别的函数多态性

node.js - 对TCP服务器进行压力测试

javascript - 带有切换功能的 jQuery 悬停功能不起作用

javascript - 如何在 Visual Studio 2005 中调试 JavaScript?

python - 如何使用 PyCharm 从命令行调试 python 脚本?

javascript - 在 Node API 中接收完整日期时间而不是仅接收日期

node.js - 向 Meteor.js 添加静态首页