首页 > 解决方案 > 如何在终端node.js中显示有错误的整行

问题描述

看到有人发了这个截图:

在此处输入图像描述

这是一个有角度的 ng 服务,显示文件中有错误的部分,我怎么能有相同的?

标签: node.jsterminal

解决方案


假设您将有一个 try/catch 片段,您可以使用以下

var stack = new Error().stack
console.log( stack ) //will print file and line plus the stack

或者

console.trace()

同样,node.js 默认会打印错误的堆栈跟踪。您可以查看所有这些行,直到找到您制作的源文件 - 这是旧样式,但在找到错误的确切来源时它不会失败。例如

Error: Something unexpected has occurred.
at main (c:\Users\Me\Documents\MyApp\app.js:9:15)  //this file would be yours
at Object. (c:\Users\Me\Documents\MyApp\app.js:17:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

我希望这有帮助


推荐阅读