如何在Node.js中打印堆栈跟踪?

有谁知道如何在Node.js中打印堆栈跟踪?

任何Error对象都有一个stack成员来捕获它构造的点。

 var stack = new Error().stack console.log( stack ) 

或者更简单:

 console.trace("Here I am!") 

现在在控制台上有一个专门的function :

 console.trace() 

如已经回答的那样,您可以简单地使用trace命令:

 console.trace("I am here"); 

但是, 如果您遇到了这个问题,请查找如何loggingexception的堆栈跟踪 ,您可以简单地loggingException对象。

 try { // if something unexpected throw new Error("Something unexpected has occurred."); } catch (e) { console.error(e); } 

它会logging:

错误:发生了意外事件。
在主(c:\ Users \ Me \ Documents \ MyApp \ app.js:9:15)
在对象。 (C:\用户\我\文件\ MyApp的\ app.js:17:1)
在Module._compile(module.js:460:26)
在Object.Module._extensions..js(module.js:478:10)
在Module.load(module.js:355:32)
在Function.Module._load(module.js:310:12)
在Function.Module.runMain(module.js:501:10)
在启动(node.js:129:16)
在node.js:814:3

如果您的Node.js版本<6.0.0 ,那么loggingException对象是不够的。 在这种情况下,它只会打印:

[错误:发生了意想不到的事情。]

对于Node版本<6,使用console.error(e.stack)而不是console.error(e)打印错误消息加上完整的堆栈,就像当前的Node版本一样。

注意:如果exception被创build为类似throw "myException"的string,则不可能检索堆栈跟踪,并且logginge.stack 未定义的

为了安全起见,你可以使用

 console.error(e.stack || e); 

它将适用于新旧的Node.js版本。

以更可读的方式在控制台中打印Error堆栈跟踪:

 console.log(ex, ex.stack.split("\n")); 

示例结果:

 [Error] [ 'Error', ' at repl:1:7', ' at REPLServer.self.eval (repl.js:110:21)', ' at Interface.<anonymous> (repl.js:239:12)', ' at Interface.EventEmitter.emit (events.js:95:17)', ' at Interface._onLine (readline.js:202:10)', ' at Interface._line (readline.js:531:8)', ' at Interface._ttyWrite (readline.js:760:14)', ' at ReadStream.onkeypress (readline.js:99:10)', ' at ReadStream.EventEmitter.emit (events.js:98:17)', ' at emitKey (readline.js:1095:12)' ] 

使用易于使用的节点模块,可以从节点获取全长堆栈跟踪(尽pipe性能损失较小): http : //www.mattinsler.com/post/26396305882/announcing-longjohn-long-stack -traces换节点-JS

对于我所知道的在nodejs中打印完整的堆栈跟踪是不可能的,你可以打印一个“部分”堆栈跟踪,你不能从代码中发现的地方看到exception发生的位置。 这就是Ryan Dahl在这个YouTubevideo中解释的。 http://youtu.be/jo_B4LTHi3I在至less56:30为准确。; 希望这可以帮助

如果您只想logging错误的堆栈跟踪(而不是错误消息),那么节点6和以上版本会自动在堆栈跟踪中包含错误名称和消息,如果您想要执行一些自定义error handling,那么有点烦人:

console.log(error.stack.replace(error.message, ''))

此解决方法将只logging错误名称和堆栈跟踪(例如,您可以将错误消息格式化并显示在代码中的其他位置)。

上面的例子只会打印堆栈跟踪后面的错误名称,例如:

 Error: at /Users/cfisher/Git/squashed/execProcess.js:6:17 at ChildProcess.exithandler (child_process.js:213:5) at emitTwo (events.js:106:13) at ChildProcess.emit (events.js:191:7) at maybeClose (internal/child_process.js:877:16) at Socket.<anonymous> (internal/child_process.js:334:11) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at Pipe._handle.close [as _onclose] (net.js:498:12) 

代替:

 Error: Error: Command failed: sh ./commands/getBranchCommitCount.sh HEAD git: 'rev-lists' is not a git command. See 'git --help'. Did you mean this? rev-list at /Users/cfisher/Git/squashed/execProcess.js:6:17 at ChildProcess.exithandler (child_process.js:213:5) at emitTwo (events.js:106:13) at ChildProcess.emit (events.js:191:7) at maybeClose (internal/child_process.js:877:16) at Socket.<anonymous> (internal/child_process.js:334:11) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at Pipe._handle.close [as _onclose] (net.js:498:12) 

您可以使用节点堆栈跟踪模块,这是一个电源满模块来跟踪调用堆栈。