How to Read a Stack Trace When You Did Not Write the Code

To read a stack trace, read the first line for what broke, then find the first frame that points at a file in your own project, not in node_modules or the runtime. That frame is where the program crashed. The bug is often one frame further down the list, in whoever called it with bad data.
A stack trace is a list of the function calls that were active when the error happened. You do not need to understand the code to read it. You need to know which direction to read and which lines to ignore.
| Language | Where the crash is | Where the original cause is |
|---|---|---|
| JavaScript | Top frame (newest first) | Innermost [cause], at the bottom |
| Python | Bottom line (oldest first) | First block, if errors are chained |
What does a JavaScript stack trace actually say?
Here is a real one. It comes from a tiny made-up app: app.js asks lib.js to format a cart total, and forgets to pass a cart. Real output from Node 22, with file paths shortened:
lib.js:2 return cart.items.reduce((s, i) => s + i.price, 0).toFixed(2); ^
TypeError: Cannot read properties of undefined (reading 'items') at Object.formatTotal (/app/lib.js:2:15) at getInvoice (/app/app.js:2:49) at Object.<anonymous> (/app/app.js:3:1) at Module._compile (node:internal/modules/cjs/loader:1781:14) at Object..js (node:internal/modules/cjs/loader:1913:10) ...Read it in four passes:
- The message.
Cannot read properties of undefined (reading 'items')is a sentence with the answer inside it. Something called.itemson a thing that wasundefined. The caret points atitems, so the thing in front of the dot,cart, is theundefinedone. - The first frame in your code.
lib.js:2:15means file, line 2, column 15. This is where it crashed. - Skip the rest of the runtime. Every
node:internal/...line is Node’s own startup machinery. Browser traces do the same with framework files. They are rarely the cause. - Walk one frame down.
formatTotaldid what it was told. The question is whycartwas undefined, and the next frame answers it:getInvoiceinapp.jsline 2 passeduser.cart, and this user never had one. The crash site and the bug are two different lines. This is the part that trips people up. A patch that only touches the top frame (say, adding a?.so it stops crashing) hides the symptom and leaves the bad data flowing.
How is a Python traceback different?
It is upside down. Python prints the oldest call first and the failure last, so you read from the bottom (the Python docs show the same layout). The last line is the error. The line above it is where it happened.
The second twist is chaining. When code catches one error and raises another, Python prints both. Real output, Python 3.10:
Traceback (most recent call last): File "a.py", line 6, in handler return load(raw)["user"]KeyError: 'user'
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "a.py", line 9, in <module> handler('{"name": "x"}') File "a.py", line 8, in handler raise RuntimeError("could not read user") from eRuntimeError: could not read userThe error you see last (could not read user) is the wrapper. The original problem, a missing user key, is the first block. When you see “direct cause”, scroll up and start with the first block. Python also chains errors without anyone writing from e: “During handling of the above exception, another exception occurred” means the same, start at the first block.
Why does JavaScript print the cause after the error?
Node does the opposite of Python. Modern JavaScript lets code attach the original error with new Error("startup failed", { cause: e }) (MDN), and Node prints it nested under the wrapper in a [cause] section. Real output again, Node 22:
Error: startup failed at start (/app/c.js:2:72) { [cause]: Error: config missing at readConfig (/app/c.js:1:49) at async start (/app/c.js:2:32)}So in JavaScript the root cause is the innermost [cause], at the bottom. In Python it is the top block. Two languages, opposite directions. If a trace looks too vague to act on, check whether there is a cause hiding under it.
What if the trace points at line 1, column 20431?
That is a production trace from minified code. Build tools squash your whole app into one or two long lines, and the frame points into that line. Here is the same crash after minifying the app (here with esbuild, though other bundlers do the same):
TypeError: Cannot read properties of undefined (reading 'items') at Object.a [as formatTotal] (/app/dist.js:1:102) at s (/app/dist.js:1:207)Function names have shrunk to a and s. A source map translates this back. On a Node server, run it with node --enable-source-maps dist.js and the same crash shows lib.js:2:15 and app.js:2:49 again (in a scratch run one function name came back wrong, which is what “best effort” means). If the flag seems to do nothing, check that the .map files are on the server next to the bundle; a build that copies only the .js files drops them. Node’s docs call this best effort and note a small speed cost if your code reads Error.stack often.
In the browser it is harder, because production source maps are off by default in Next.js: its docs say they are disabled to avoid leaking your source (productionBrowserSourceMaps). Turning them on publishes your source, which is the tradeoff in the leaked keys post. The usual way out is to upload the maps privately to your error tracker instead of serving them. Sentry’s docs describe this for webpack, Rollup, Vite and esbuild.
What do the traces in my browser look like?
Most vibe-coded apps fail in the browser, not in Node. Open the browser console (your tool’s error panel may show only a summary) to see the full trace. In production React, an error may read “Minified React error #NNN” with a number. react.dev/errors decodes it, because the production build leaves out the full messages to save bytes. A trace can also look too short when it crosses an await: Node marks those frames at async, as in the [cause] example above, and frames from older callback code may be missing.
What does a test for this look like?
A trace tells you where to look. A test makes it a reproducible failure. Here is the invoice app as two small files, plus a test that describes what a user should see:
function formatTotal(cart) { return cart.items.reduce((s, i) => s + i.price, 0).toFixed(2);}module.exports = { formatTotal };const { formatTotal } = require('./lib');function getInvoice(user) { return `Invoice for ${user.name}: $${formatTotal(user.cart)}`; }module.exports = { getInvoice };const test = require('node:test');const assert = require('node:assert');const { getInvoice } = require('../app');
test('an invoice for a user with items shows their total', () => { const user = { name: 'Ana', cart: { items: [{ price: 5 }, { price: 7.5 }] } }; assert.strictEqual(getInvoice(user), 'Invoice for Ana: $12.50');});test('an invoice for a user who never had a cart shows $0.00', () => { assert.strictEqual(getInvoice({ name: 'Bo' }), 'Invoice for Bo: $0.00');});Run node --test (Node 18 or newer). The second test fails with the same message as the trace:
✔ an invoice for a user with items shows their total✖ an invoice for a user who never had a cart shows $0.00 TypeError: Cannot read properties of undefined (reading 'items')ℹ pass 1ℹ fail 1Now try the patch that only touches the top frame, cart?.items in formatTotal. The crash stops, but the test is still red, with a worse answer: actual: 'Invoice for Bo: $undefined', expected $0.00. The fix belongs in the caller, which passes a missing cart. Revert the patch and change app.js to formatTotal(user.cart ?? { items: [] }). Both tests pass (pass 2, fail 0). The test checks what the user sees, so it would also catch a different fix that returns the wrong text.
What do I give the AI?
Copy the entire trace, from the first message to the last frame, plus the caller’s code. Pasting only the red last line throws away the frames that carry the answer. First scan it for secrets: database errors can include a connection string, and URLs can carry tokens. The logs post covers redaction. Then paste this:
Here is a stack trace and the files named in it. Do not fix anything yet. For each frame that points at my own code, say in one plain sentence what that function was doing. Then tell me which frame passed in the bad value, not just which one crashed, and why. If any frame is minified or from a library, say so and skip it.
Not sure your theory about the cause is right? Rubber duck debugging with an AI shows how to let a test settle it. Once you know where the bug is, Stop pasting the error back covers what to do when the fix does not work, and Your AI fixed one bug and broke two covers what to do when it works too well.
The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.