You can configure the logged error stack trace length in certain environments

2 min read

This post is part of my Today I learned series in which I share all my learnings regarding web development.

Debugging JavaScript apps can be hard. Today I was reading an article about debugging functional JavaScript and found a handy tiny detail.

Let's assume an error is thrown deep inside your app.

main();

function main() {
  one();
}

function one() {
  two();
}

function two() {
  three();
}

// you get the idea :)

function ten () {
  eleven()
}

function eleven () {
  throw new Error("I can't see the whole stack strace")
}

When you execute this in Chrome or Node.js you get the following stack trace printed to the console.

Uncaught Error: I can't see the whole stack strace
    at eleven (:49:9)
    at ten (:45:3)
    at nine (:41:3)
    at eight (:37:3)
    at seven (:33:3)
    at six (:29:3)
    at five (:25:3)
    at four (:21:3)
    at three (:17:3)
    at two (:13:3)

As you see the first two function calls (main and one) of the stack trace are omitted. It turns out that you can configure the length of the printed stack trace using Error.stackTraceLimit. This configuration enables you to enrich the logging when you're facing a bug buried deep in your application.

Error.stackTraceLimit = 20;

When you increase this value, you will see the whole trace logged.

Uncaught Error: I can't see the whole stack strace
    at eleven (:50:9)
    at ten (:46:3)
    at nine (:42:3)
    at eight (:38:3)
    at seven (:34:3)
    at six (:30:3)
    at five (:26:3)
    at four (:22:3)
    at three (:18:3)
    at two (:14:3)
    at one (:10:3)
    at main (:6:3)
    at :2:1

If you use Error.stackTraceLimit you have to be aware of the fact that it is a non-standard according to MDN and a quick check reveals that is also not supported in Firefox. I didn't check Edge.

Also, Chrome and Safari support it but use different default values. Chrome goes with 10 and Safari with 100. When you look around you'll also see that CodeSandbox increases the limit to 50.

In my opinion, this configuration is nothing ground-breaking, but it might become handy someday when I'll debug a large JS app in the browser or a Node.js application. It won't replace the debugger though. :)

Load time