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

node.js堆栈错误超过10行?,node.js,stack-trace,Node.js,Stack Trace,有没有办法在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() {

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

有没有办法拨打10个以上的电话?

使用该模块。

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

Error.stackTraceLimit = Infinity;

如果您希望看到跨越setTimeout/setInterval调用的堆栈跟踪,那么更复杂的方法就是这样。

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

节点——堆栈跟踪限制=1000 debug.js
//默认值10

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

节点——堆栈大小=1024 debug.js
//默认值492

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

要查看所有与堆栈相关的选项,请执行以下操作:

节点--v8选项| grep-B0-A1堆栈

您也可以使用,它打开了熟悉的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/...

您可以在
NODE\u 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
$NODE_OPTIONS=--stack trace limit=100 NODE debug.js
ReferenceError:未定义dieInHell
在a(/tmp/debug.js:1:16)
在b(/tmp/debug.js:2:16)
在c(/tmp/debug.js:3:16)
在d(/tmp/debug.js:4:16)
在e(/tmp/debug.js:5:16)
在f(/tmp/debug.js:6:16)
在g(/tmp/debug.js:7:16)
在h(/tmp/debug.js:8:16)
在i(/tmp/debug.js:9:16)
在j(/tmp/debug.js:10:16)
在k(/tmp/debug.js:11:16)
在l(/tmp/debug.js:12:16)
在m(/tmp/debug.js:13:16)
在n(/tmp/debug.js:14:16)
at o(/tmp/debug.js:15:16)
在p(/tmp/debug.js:16:16)
在q(/tmp/debug.js:17:16)
反对。(/tmp/debug.js:20:5)
at模块编译(节点:内部/modules/cjs/loader:1108:14)
在Object.Module._extensions..js(节点:internal/modules/cjs/loader:1137:10)
at Module.load(节点:内部/模块/cjs/加载器:973:32)
at功能模块加载(节点:内部/模块/cjs/加载程序:813:14)
在Function.executeUserEntryPoint[作为runMain](节点:内部/modules/run_main:76:12)
节点处:内部/主/运行\u主\u模块:17:47

更好的调试是Node.js未来版本的首要任务。我是否从您的评论中得知这还不能完成?没有。但是更好的调试在清单上。6:)让我们希望Node.js最终默认集成这个--堆栈跟踪限制从0.10.22起仍然有效,谢谢!从Node.js v8.0.0开始,您还可以在
Node\u OPTIONS
环境变量中设置此选项,例如
Node\u OPTIONS='--stack trace limit=10000'/path/to/some script
。如果您没有直接调用
节点
,则非常有用。上次尝试时,Error.stackTraceLimit没有为我执行此操作。请注意,某些包可能
stackTraceLimit
。而且,它只会影响我所看到的
Error.stack
中的内容。内置调试器总是显示完整的堆栈(
bt
command)。显然,堆栈跟踪不遵循异步操作。换句话说,在异步调用的回调中,堆栈从零开始(基本上是空的)。@x-yuri Node.js团队正在处理该问题(),否则,当您使用
--inspect
--inpect brk
commandOmg运行节点应用程序时,在Chrome调试器中可以看到完整的堆栈。这让我抓狂。谢谢你的信息!