Skip to content
Happy Programming Guide
Start learning
Debugging & Errors

How to Read a Stack Trace

A stack trace is a list of the function calls that led to an error. Here is how to read one in Python and JavaScript, and how to find the line that is actually yours.

A stack trace is the trail of function calls that led to an error. It looks intimidating because it is long, but you only need two lines from it.

A Python traceback#

Output
Traceback (most recent call last):
  File "app.py", line 20, in <module>
    report = build_report(orders)
  File "app.py", line 14, in build_report
    return summarise(items)
  File "app.py", line 8, in summarise
    average = total / len(items)
ZeroDivisionError: division by zero

Read it like this:

  • Bottom line — the error type and message. Start here.
  • Line directly above it — the exact line that failed: average = total / len(items).
  • Lines above that — how the program got there, oldest at the top.

Python literally tells you the order: “most recent call last”. The bottom is the scene of the crash; the top is where the journey started.

Here, items was empty. The fix is not at line 8 — it is deciding what an empty order list should mean, probably at line 20.

A JavaScript stack trace#

Output
TypeError: Cannot read properties of undefined (reading 'name')
    at showUser (app.js:12:20)
    at handleClick (app.js:5:3)
    at HTMLButtonElement.<anonymous> (app.js:2:24)

JavaScript is the other way round: the error is at the top, and the most recent call is on the first at line. Read downward to see how you arrived.

app.js:12:20 means file app.js, line 12, column 20.

Finding your code in a long trace#

When a library is involved, the trace can be forty lines. Scan for the first entry containing a file you wrote — that is almost always where the real problem is.

Output
    at Array.map (<anonymous>)
    at renderList (app.js:34:18)        <-- your file, start here
    at node_modules/react-dom/...

Library code is usually correct. What is wrong is the data you handed it.

Chained errors in Python#

Output
ValueError: invalid literal for int() with base 10: 'abc'

During handling of the above exception, another exception occurred:

KeyError: 'total'

Two errors: one happened while handling another. The first one is usually the real cause; the second is often a broken error handler. Fix the top one first.

You will also see “The above exception was the direct cause of the following exception”, which means the same thing — start at the top.

Making traces useful in your own code#

Do not swallow the trace:

Python
try:
    process(data)
except Exception:
    print("Something went wrong")     # you have thrown away the evidence

import traceback
try:
    process(data)
except Exception:
    traceback.print_exc()             # keeps the full trace

In JavaScript, log the whole error object rather than just its message — console.error(error) keeps the stack, console.log(error.message) does not.

Minified traces#

In a production build you may see at t (main.a1b2.js:1:4820), which is useless. Source maps translate that back to your original files; most build tools produce them, and DevTools uses them automatically if they are available.

Questions people ask#

What does “<module>” mean in a Python trace?

The code was at the top level of the file rather than inside a function. It marks the outermost frame.

Why is my trace missing lines?

Async code can break the chain, because the call happened in an earlier tick of the event loop. Modern engines reconstruct much of it, but async traces are still less complete.

What is the difference between an error and an exception?

In everyday use they are the same thing. Python says exception, JavaScript says error, and both mean “something went wrong and the normal flow stopped”.

Where to go next#

Next lessonWhy is my CSS not working?

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *