node.js 堆栈错误中超过 10 行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7697038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:40:10  来源:igfitidea点击:

More than 10 lines in a node.js stack error?

node.jsstack-trace

提问by Julien Genestoux

Is there a way to get more than 10 lines in a node.js stack error?

有没有办法在 node.js 堆栈错误中获得超过 10 行?

function a() { dieInHell(); }
function b() { a(); }
function c() { b(); }
function d() { c(); }
function e() { d(); }
function f() { e(); }
function g() { f(); }
function h() { g(); }
function i() { h(); }
function j() { i(); }
function k() { j(); }
function l() { k(); }
function m() { l(); }
function n() { m(); }
function o() { n(); }
function p() { o(); }
function q() { p(); }

try {
    q();
}
catch(e) {
    console.log(e.stack);
}

shows :

显示:

$ node debug.js 
ReferenceError: dieInHell is not defined
    at a (/Users/julien/tmp/debug.js:2:5)
    at b (/Users/julien/tmp/debug.js:6:5)
    at c (/Users/julien/tmp/debug.js:10:5)
    at d (/Users/julien/tmp/debug.js:14:5)
    at e (/Users/julien/tmp/debug.js:18:5)
    at f (/Users/julien/tmp/debug.js:22:5)
    at g (/Users/julien/tmp/debug.js:26:5)
    at h (/Users/julien/tmp/debug.js:30:5)
    at i (/Users/julien/tmp/debug.js:34:5)
    at j (/Users/julien/tmp/debug.js:38:5)

Is there a way to get more than 10 calls?

有没有办法接到 10 个以上的电话?

回答by Mariusz Nowak

Easiest solution for that is to start your code with following:

最简单的解决方案是使用以下代码启动您的代码:

Error.stackTraceLimit = Infinity;

If you'd like to see stack trace that spans over setTimeout/setInterval calls, then more sophisticated https://github.com/mattinsler/longjohnwould be the way to go.

如果您想查看跨越 setTimeout/setInterval 调用的堆栈跟踪,那么更复杂的https://github.com/mattinsler/longjohn将是您的最佳选择。

回答by jakub.g

You can pass stack trace limit as a command line param to node:

您可以将堆栈跟踪限制作为命令行参数传递给node

node --stack-trace-limit=1000 debug.js// default 10

node --stack-trace-limit=1000 debug.js// 默认 10

BTW, another thing which sounds unlikely to happen, but just wasted a few hours of my time for debugging, is the stack size (which defaults to 492 kB). You can have very uninformative errors if the stack is exhausted (RangeErrorwithout any additional info). You can increase the stack size with:

顺便说一句,另一件事听起来不太可能发生,但只是浪费了我几个小时的调试时间,是堆栈大小(默认为 492 kB)。如果堆栈耗尽(RangeError没有任何其他信息),您可能会遇到非常无意义的错误。您可以使用以下方法增加堆栈大小

node --stack-size=1024 debug.js// default 492

node --stack-size=1024 debug.js// 默认 492

In the world of callback-to-callback-to-callback chainings, it's in fact very easy to exceed the stack size for big input sizes, if the program is not written in this in mind.

在回调到回调到回调链的世界中,实际上很容易超过大输入大小的堆栈大小,如果程序没有考虑到这一点。

To see all stack-related options:

要查看所有与堆栈相关的选项:

node --v8-options | grep -B0 -A1 stack

node --v8-options | grep -B0 -A1 stack

回答by zbycz

Also you can use built-in debugger, which opens familiar Google Chrome's dev-tools debugger. It stops on any error and you can browse the whole stack. Just run:

您也可以使用内置调试器,它会打开熟悉的 Google Chrome 的开发工具调试器。它会在出现任何错误时停止,您可以浏览整个堆栈。赶紧跑:

$ node --inspect debug.js

Debugger listening on port 9229.
To start debugging, open the following URL in Chrome: chrome-devtools://devtools/remote/serve_file/...