捕捉node.js未定义的variables

在浏览器中使用正常的Javascript当您尝试使用未定义的variables时,Google Chrome将在控制台中显示错误。 然而在Node.js中没有显示错误,我想使用undefinedvariables的函数退出。

我已经添加:

process.on('uncaughtException', function(err) { console.log( 'uncaughtException :', err ); }); 

但是这不被执行。

我也在调用函数中添加了try-catch,并没有引发exception。

我正在使用:“严格使用”;

所以我的问题是,有没有办法我可以得到/看到一个错误,当一个未定义的variables被访问?

节点将自动执行此操作。

 // 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) 

我没有看到这个exception的原因是其他exception处理程序代码正在节点之前。 请参阅我上面的回复。 MongoDB驱动程序。

欢迎任何其他build议。 这是捕捉exception的代码:

  catch (err) { var errorObject = {err:"socketHandler", trace:err, bin:self.buffer, parseState:{ sizeOfMessage:self.sizeOfMessage, bytesRead:self.bytesRead, stubBuffer:self.stubBuffer}}; if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject); // We got a parse Error fire it off then keep going self.emit("parseError", errorObject, self); } 

内维尔