Developer tools
Stack trace (error.stack)
A printout of who called whom when the error happened: read it top to bottom, from the crash site back to the start of the program.
Why it exists
The message "Cannot read properties of undefined" on its own is almost useless: a big program reads properties in thousands of places. The stack answers the two main debugging questions — where exactly it crashed (file, line, column) and how we got there (which functions called each other). Without it you'd be guessing, or sprinkling console.log all over the project.
Where it's used
- Red text in the browser console and in the Node.js terminal: the first thing you read after any crash.
- Error monitoring services (Sentry, Rollbar, LogRocket): they send developers the stack of every crash that happens to users.
- Bug reports and forum questions: the stack is the key piece of evidence; without it, helping is hard.
- Server logs: on an unhandled error, Node.js prints the stack and only then stops.
Syntax
error.stack // a string: "Type: message" + lines "at function (file:line:column)"
console.trace("label") // print the current stack without throwing anything
new Error("text", { cause: error }) // wrap an error and keep the originalExamples
function inner() {
throw new TypeError("No data");
}
function outer() {
inner();
}
let firstLine = "";
try {
outer();
} catch (error) {
firstLine = error.stack.split("\n")[0];
}
firstLine; // → "TypeError: No data"function inner() {
throw new Error("Broken");
}
function outer() {
inner();
}
let stack = "";
try {
outer();
} catch (error) {
stack = error.stack;
}
stack.includes("at inner"); // → true
stack.indexOf("at inner") < stack.indexOf("at outer"); // → true — the innermost call is on topfunction makeError() {
return new Error("Created here");
}
const error = makeError(); // the stack is captured at creation
typeof error.stack; // → "string"
error.stack.includes("makeError"); // → true — even without throwResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
At the moment an error object is created (new Error(...), or when the engine creates a TypeError itself), the engine takes a snapshot of the call stack — the pile of unfinished calls from the recursion chapter. Each entry becomes a line at name (file:line:column). The innermost call (where the error was created) is on top, below it is whoever called it, and so on down to the start of the program.
The stack property is non-standard, but every browser and Node.js has it. The format differs: Chrome and Node write at inner (app.js:2:9), Firefox and Safari write inner@app.js:2:9. By default V8 keeps the top 10 entries. For async code V8 rebuilds the chain across await and marks those lines with the word async.
If the code is bundled and minified (a one-line main.min.js), the line and column point into the minified file. To show the original sources, DevTools and Sentry use source maps — dictionary files mapping "position in the bundle → line in the source".
Traps
How it differs from…
throwandErrorthrow sends an error up the call stack until someone catches it.
stackis the record of where the error was created and how we got there.
When to pick something else
Don't parse error.stack in program logic (for example, to find out who called a function): the format isn't standardized and differs between browsers. And don't show the stack to users — it reveals how the code is built; users need a clear message, and the stack belongs in the logs.