How to Read a Python & JavaScript Stack Trace
When an unhandled exception crashes your program, the terminal prints a wall of text. It looks intimidating at first glance, but it tells you exactly what went wrong and where. Here is how to read it in seconds.
The Fundamental Rule of Stack Traces
In Python, the last line names the exception and the exact error message. The line immediately above it is where execution crashed.
In JavaScript / Node.js, the first line names the Error type. The top stack frame listed immediately underneath is where the exception was thrown.
1. Anatomy of a Python Traceback
Consider this real traceback produced when calculating a user discount:
Traceback (most recent call last):
File "server.py", line 42, in handle_checkout
receipt = calculate_total(cart, user_id)
File "pricing.py", line 18, in calculate_total
discount = user["tier"]["discount_pct"]
KeyError: 'tier'Line 6: KeyError: 'tier' โ This tells you Python tried to look up the key 'tier' in a dictionary, but that key does not exist.
Lines 4โ5: File "pricing.py", line 18 โ The exact line in your code where the dictionary lookup occurred.
Lines 2โ3: File "server.py", line 42 โ The caller function that invoked calculate_total.
2. Anatomy of a JavaScript Stack Trace
In modern browsers or Node.js, the order is inverted:
TypeError: Cannot read properties of undefined (reading 'discount_pct')
at calculateTotal (pricing.js:18:24)
at handleCheckout (server.js:42:15)
at processTicksAndRejections (node:internal/process/task_queues:95:5)Line 1: The error class (TypeError) and the message. In JavaScript, reading a property on something that is undefined immediately throws this error.
Line 2: The top frame (pricing.js:18:24) points to line 18, column 24 where the dereference failed.
Line 4: Internal Node.js or runtime frames (node:internal/...). You can ignore these internal frames โ focus exclusively on the lines pointing to your project files!
3. The 3-Step Root Cause Strategy
- Ignore library frames: Scroll past framework internals (such as
express,react-dom, or standard libraries) until you spot the last file you authored. - Identify the operand:Read the error message literally. If it says "list index out of range", the bug is the index value. If it says "NoneType object has no attribute", find why the object was None.
- Inspect before the crash: Add a log statement or step through right before the failing line. The bug is rarely the line that crashed; it is usually the assignment several lines earlier that set the wrong value.
Want to practice spotting bugs in live code?
Explore 69+ detailed error guides or jump into our interactive browser-based challenges.