Stack trace

A stack trace is the list of function calls that were in progress when an error was created, one call per line, each with its file, line and column. The top line is where the error was raised and each line below it is the call that led there, so the trace records the path the program took to the failure.

The longer version

Every running program keeps a call stack. Microsoft's debugger documentation describes it like this: each function call pushes a stack frame, holding the return address, the parameters and the local variables, onto the top of the stack, and the frame is popped off when the function returns. A stack trace is a written copy of that stack at one moment, usually the moment an error object was created. In JavaScript the copy is the error's stack property. MDN notes that the property is part of no standard, though all major engines implement it, and that its exact content differs between them. All of them put one call on each line, with the call that directly caused the error at the top and the call that started the chain at the bottom. V8, the engine in Chrome, Edge and Node.js, writes each frame as at functionName (file:line:column) and, according to its documentation, keeps the topmost 10 frames by default, a number Error.stackTraceLimit changes. Firefox and Safari write functionName@file:line:column. Java prints its trace top-down too, with Caused by sections for wrapped exceptions, while a Python traceback announces most recent call last and puts the failing call at the bottom.

A JavaScript stack trace is read from the top, one question per line. Take this one from Chrome: TypeError: Cannot read properties of undefined (reading 'price'), then at lineTotal (basket.js:42:31), at Array.map (<anonymous>), at renderBasket (basket.js:18:24) and at HTMLButtonElement.onQuantityChange (basket.js:7:5). The first line gives the error type and the message: some code read price from a value that was undefined. The top frame says where, line 42, column 31 of basket.js, inside lineTotal. The frame marked <anonymous> is the browser's own map method and has no file to open. Reading down gives the route: a quantity change on a button called renderBasket, which mapped over the basket lines and called lineTotal for each one. The top frame is where the error surfaced, which is often a step after its cause. Here the real question is why one basket line had no product, and the answer is in renderBasket or in the data it was given. Frames from libraries and frameworks are usually skipped, and the first frame in the application's own code is where to start.

Two things make a trace from a real web page shorter or harder to read than that one. The first is asynchronous code. A callback that runs later, after a timer, a network response or an event, starts on a fresh stack, so its trace ends at the event loop with no record of the click that scheduled it. V8 has added async frames, marked at async functionName, by default since version 7.3, and its documentation says they cover only await, Promise.all() and Promise.any(), so a chain of then callbacks or a setTimeout still loses the frames before the gap. A promise rejected with a plain string carries no stack at all. The second is minification. A production bundle renames functions and puts most of its code on one line, so a frame reads like at t (app.3f9c1.js:1:48211), where only the column says anything. A source map links that position back to the original file and line. Chrome DevTools uses one when source maps are enabled in its settings, and its documentation says errors, logs and breakpoints then map to the original files automatically. A trace copied as text from a user's browser stays minified, so it has to be mapped with the source map of the same build that produced it.

Because a trace names files, functions and often library versions, showing one to visitors is a weakness MITRE catalogues as CWE-209, Generation of Error Message Containing Sensitive Information: a production error page should say something short and generic and leave the full trace to logs the team controls. For a tester the trace is evidence, and three habits keep it useful in a bug report. Copy it whole, as text, with the message line, because a cropped screenshot loses frames and cannot be searched. Give the browser and the build or version, since the format depends on the engine and a minified trace can only be mapped with that build's source map. And write down what the user did just before, which the trace may not show once an async gap has cut it off. The trace says where the code failed, the report says what the person was doing, and a developer needs both.

Common questions

What is a stack trace in simple terms?

A stack trace is a list of the functions that were running when something went wrong, from the one where the error happened back to the one that started the chain. Each line names a function and the file, line and column it had reached, so a developer can follow the route to the failure.

What is an error stack trace?

An error stack trace is the stack trace recorded on an error object when it is created, listing the calls in progress at that moment. In JavaScript it is the error's stack property, which the console prints under the error message. A thrown exception carries one, and so does a rejected promise whose reason is an Error.

How do you read a stack trace in JavaScript?

Start with the first line, which gives the error type and the message. The next line is the top frame: the function and position where the error was raised. Each line after it is the caller of the one above. Skip frames from libraries and native code such as <anonymous>, and open the first frame that points to the application's own source.

Why is my stack trace minified or unreadable?

Production bundles are minified, so function names are shortened and most of the code sits on one line, which leaves frames like at t (app.js:1:48211). A source map for the same build maps each position back to the original file and line. Browser developer tools apply it automatically when the map is available and source maps are enabled.

Why is the stack trace so short in async code?

A callback that runs after a timer, an event or a network response starts on a fresh stack, so the calls that scheduled it are no longer there. V8 records async frames, marked at async, only for await, Promise.all() and Promise.any(). Code built on then callbacks or setTimeout can lose everything before the gap, so a report should say what the user did.

How do you get a stack trace in JavaScript without an error?

Create an error and read its stack property, as in new Error().stack, or call console.trace(), which prints the current call stack to the console without throwing anything. Both show how the code reached that point, which helps when the question is which part of the page calls a function.

Is it safe to show a stack trace to users?

Showing a stack trace to visitors leaks information, a weakness MITRE catalogues as CWE-209, because the trace reveals file paths, function names and library versions. Production error pages should show a short generic message and keep the full trace in server logs or an error-monitoring tool the team controls.

What is the difference between a stack trace and a call stack?

The call stack is the live structure a running program keeps, one frame per function call in progress, growing and shrinking as functions are called and return. A stack trace is a written copy of it taken at one moment, usually when an error is created, and it survives after the program has moved on.

Bug reports that arrive ready to triage.

Collect replay, console, network and browser context with the Free plan.

<script src="…/widget.js" data-key="pb_pub_…">