131

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

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?

Maen
  • 10,603
  • 3
  • 45
  • 71
Julien Genestoux
  • 31,046
  • 20
  • 66
  • 93

5 Answers5

188

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/longjohn would be the way to go.

jsalonen
  • 29,593
  • 15
  • 91
  • 109
Mariusz Nowak
  • 32,050
  • 5
  • 35
  • 37
  • 2
    Error.stackTraceLimit didn't do it for me when I last tried. – B T Feb 28 '17 at 23:57
  • Note that some packages might [change](https://github.com/webpack/webpack/blob/v3.8.1/bin/webpack.js#L331) `stackTraceLimit`. Also, it affects only what you get in `Error.stack` from what I can see. Built-in debugger always displays full stack (`bt` command). – x-yuri Nov 17 '17 at 18:32
  • And apparently, stack trace doesn't follow async operations. In other words, in a callback of an asynchronous call your stack starts from scratch (it's basically empty). – x-yuri Nov 17 '17 at 18:44
  • @x-yuri Node.js team is working on that ( https://github.com/nodejs/node/issues/11865 ) Otherwise full stack is visible in Chrome debugger when you run Node applicaiton with `--inspect` or `--inpect-brk` command – Mariusz Nowak Nov 21 '17 at 10:48
  • Omg this was driving me crazy. Thanks for this info! – Kris Oye Jan 24 '18 at 10:25
  • I like about JS that you can use **Infinity** as a number. – gregn3 Jan 23 '20 at 20:08
88

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

node --stack-trace-limit=1000 debug.js // default 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 (RangeError without any additional info). You can increase the stack size with:

node --stack-size=1024 debug.js // default 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

jakub.g
  • 38,512
  • 12
  • 92
  • 130
  • 3
    --stack-trace-limit still working as of 0.10.22, thanks! – Riplexus Jan 29 '14 at 18:58
  • 8
    As of Node.js v8.0.0, you can also set this in the `NODE_OPTIONS` environment variable, e.g. `NODE_OPTIONS='--stack-trace-limit=10000' /path/to/some-script`. Useful if you're not invoking `node` directly. – Bluu Sep 05 '18 at 05:30
10

You can set the trace limit within NODE_OPTIONS variable:

$ 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
radrow
  • 6,419
  • 4
  • 26
  • 53
7

Use the https://github.com/tlrobinson/long-stack-traces module.

3rdEden
  • 4,388
  • 1
  • 20
  • 18
0

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/...
zbycz
  • 626
  • 9
  • 12