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

有没有办法在 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);
}

图示:

$ 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?

40368 次浏览

最简单的解决方案是以下面的代码开始:

Error.stackTraceLimit = Infinity;

如果您希望看到跨 setTimeout/setInterval 调用的堆栈跟踪,那么可以使用更复杂的 https://github.com/mattinsler/longjohn

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

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

顺便说一句,另一件听起来不太可能发生的事情,只是浪费了我几个小时的调试时间,是 堆栈大小(默认为492kB)。如果堆栈用完了,您可能会出现非常无用的错误(RangeError没有任何附加信息)。可以使用:

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

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

To see all stack-related options:

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

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:

$ node --inspect debug.js


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

可以在 NODE_OPTIONS变量中设置跟踪限制:

$ NODE_OPTIONS=--stack-trace-limit=100 node debug.js
ReferenceError: dieInHell is not defined
at a (/tmp/debug.js:1:16)
at b (/tmp/debug.js:2:16)
at c (/tmp/debug.js:3:16)
at d (/tmp/debug.js:4:16)
at e (/tmp/debug.js:5:16)
at f (/tmp/debug.js:6:16)
at g (/tmp/debug.js:7:16)
at h (/tmp/debug.js:8:16)
at i (/tmp/debug.js:9:16)
at j (/tmp/debug.js:10:16)
at k (/tmp/debug.js:11:16)
at l (/tmp/debug.js:12:16)
at m (/tmp/debug.js:13:16)
at n (/tmp/debug.js:14:16)
at o (/tmp/debug.js:15:16)
at p (/tmp/debug.js:16:16)
at q (/tmp/debug.js:17:16)
at Object.<anonymous> (/tmp/debug.js:20:5)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47