Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the entire stack trace in Node

Is it possible to get the entire stack trace in Node? The following script should print 100 items, but only prints 10. Note, it does work in Chrome, just not in Node. (To run the snippet below and see results, you'll actually need to open the browser's dev tools.)

function trace(depth = 100) {
  if (!depth) {
    console.trace();
  } else {
    trace(depth - 1);
  }
}

trace();

I've also tried the new Error().stack method, but this prints the same limited number of lines. Unlike stack.trace() this method also only displays 10 items in Chrome.

function trace(depth = 100) {
  if (!depth) {
    console.info(new Error().stack);
  } else {
    trace(depth - 1);
  }
}

trace();

The debugger knows the entire stack trace, not to mention the JS engine itself needs to be able to walk the stack for closures, so there must be a way.

like image 469
dx_over_dt Avatar asked Sep 17 '26 18:09

dx_over_dt


1 Answers

In your code, you need to declare:

Error.stackTraceLimit = Infinity;

This works for both methods in the OP.

like image 100
mdrichardson Avatar answered Sep 19 '26 07:09

mdrichardson