How to trace a bug from a stack trace in JavaScript and TypeScript
Read a Node.js or browser stack trace properly: find your frames, use source maps, understand async gaps, and work back from the error to its real cause.
A stack trace is a map of how execution reached an error. Read properly, it often points straight at the cause. Read carelessly, it sends you into framework internals. Here's how to get the most out of one.
Anatomy of a stack trace
TypeError: Cannot read properties of undefined (reading 'price')
at lineTotal (src/pricing.ts:14:22)
at Array.map (<anonymous>)
at calculateTotals (src/pricing.ts:31:18)
at createOrder (src/services/orders.ts:52:20)
at async POST (app/api/orders/route.ts:18:17)- The first line is the error type and message. "Cannot read properties of undefined (reading 'price')" means something expected to be an object was
undefined, and the code tried to read.pricefrom it. - The top frame is where the error was thrown:
src/pricing.tsline 14, column 22. - Lower frames are the callers, down to where execution started.
Find your frames
Ignore frames in node_modules and runtime internals at first. Your code is usually a few frames down. The top frame in your code is where to look; the frames below it show how bad data got there.
The error is rarely where the bug is
The top frame shows where the program noticed a problem. The bug is often earlier: here, lineTotal received an item without a product. Why? Look at calculateTotals (line 31), then createOrder (line 52). Perhaps an item was added to the cart and the product was deleted later. Walk down the stack, asking at each frame "where did this value come from?"
Source maps
If the trace points at compiled files like dist/server.js:1:48291 or a minified bundle, you need source maps to translate locations back to source.
- In Node.js, run with
--enable-source-maps(or setNODE_OPTIONS=--enable-source-maps). - In browsers, dev tools apply source maps automatically if they're published or available locally.
- Error-tracking services can apply uploaded source maps to production errors.
Async gaps
With async/await, modern Node and browsers keep async frames (marked async in the trace), but some boundaries still lose the chain: callbacks from timers, event emitters and some libraries. If the trace ends abruptly at an event handler, search for where that event is emitted to continue tracing.
Error messages worth knowing
| Message | Usual meaning |
|---|---|
| Cannot read properties of undefined | A value you expected is missing; check where it was set |
| X is not a function | Wrong import (default versus named), or a value was overwritten |
| Maximum call stack size exceeded | Infinite recursion, often two functions calling each other |
| Unhandled promise rejection | An await or .catch is missing |
| ECONNREFUSED / ETIMEDOUT | A service you call isn't reachable |
Reproduce from the trace
With file, line and the likely bad value, write a small test that calls the function with that value. If it throws the same error, you've reproduced the bug in isolation.
Ask with the trace
You can paste a stack trace into RepoGrasp along with the repository link and ask "what could cause this error, and where does the missing value come from?" It reads the files in the trace and their callers, and cites each one. Use the answer as a list of places to look, then confirm with the debugger.