Home > Enterprise >  How do I get Node to break on a stack overflow?
How do I get Node to break on a stack overflow?

Time:06-19

Let's say I have a JS script like the following:

function recurse() {
    return recurse();
}
recurse();

If I run node recurse.js, it will of course fail with a "Maximum call stack size exceeded" error.

How do I get a debugger (for example, running in VS Code) to break before this exception happens, so I can see what's in the stack? I can attach a breakpoint, but it would take forever to hit this exception.

CodePudding user response:

Maximum call stack size exceeded is an exception like any other. Wrap the call in a try/catch, and add a stop point inside the catch block, like so:

function recurse() {
    return recurse()
}

try {
  recurse()
} catch(e) {
  console.log(e) // <- stop here
}
  • Related