Back to writing

How to read an error message

Red text can feel like the computer telling you off, but an error tells you what went wrong and where. Here is how to read one, a line at a time.

A hand holds a magnifying glass up to a laptop screen full of blurred code. Inside the lens, one word is sharp and glowing red: TypeError.
Image © 2026 Chiranjit Karmakar. All rights reserved.

When you’re new to programming, red text in the terminal feels like being told off. So most beginners do one of three things: scroll past it, change random lines until it goes away, or paste the whole thing into a search box.

But an error message is the most useful thing your computer will say to you all day. It tells you what went wrong, and where. It even tells you how the program got there. You just have to know which part to read first.

Start with the type and the message

Here is a small Python program that adds tax to some prices:

def total(items):
    tax = "18"
    return sum(items) + tax

prices = [120, 80, 45]
print(total(prices))

Run it, and you get this:

Traceback (most recent call last):
  File "shop.py", line 6, in <module>
    print(total(prices))
          ~~~~~^^^^^^^^
  File "shop.py", line 3, in total
    return sum(items) + tax
           ~~~~~~~~~~~^~~~~
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Python puts the most important line at the bottom, so start there. It has two parts:

  • The type, TypeError. This is the kind of problem. A TypeError means a value was the wrong kind of thing for what you did with it.
  • The message, after the colon. Here it says you used + with a whole number (int) and some text (str). Python doesn’t know how to add 200 and "18".

Before you look at any code, you already know a lot. Somewhere, a number and some text met at a +.

Then find where it happened

Everything above the last line is the traceback. It lists the function calls that were running when the program stopped. The words most recent call last tell you the order: the call at the bottom is where it broke, and each one above it is how the program got there.

Read it from the bottom up:

  1. line 3, in total: the error happened on line 3, inside total. Python even underlines the +.
  2. line 6, in <module>: total was called from line 6, at the top level of the file.

Line 3 adds sum(items), a number, and tax. So tax must be the text. One line up, there it is: tax = "18", in quotes. Remove the quotes and the program works.

JavaScript puts it the other way up

Here is a similar mistake in JavaScript:

const users = [{ name: "Asha" }, { name: "Ravi" }];

function greet(user) {
  return "Hello, " + user.name;
}

console.log(greet(users[2]));

Node prints this. I’ve shortened the file paths and cut some lines, which I’ll explain below:

app.js:4
  return "Hello, " + user.name;
                          ^

TypeError: Cannot read properties of undefined (reading 'name')
    at greet (app.js:4:27)
    at Object.<anonymous> (app.js:7:13)

This time the type and message come first, and the list of calls goes below them, with the most recent at the top. The browser console shows the same message and list.

The numbers after the file name are the line and the column: app.js:4:27 means line 4, character 27. That’s the . in user.name, which is where Node points its ^.

Now read the message carefully, because beginners often read it the wrong way round. Cannot read properties of undefined (reading 'name') doesn’t mean something is wrong with name. It means the value before .name is undefined. So user is undefined.

Why? The next line of the list says greet was called from line 7, with users[2]. The list has two users, at positions 0 and 1. There is nothing at position 2, so users[2] is undefined, and greet received nothing.

The error is where the program noticed

This is the most important idea in this post. Both errors were reported on one line, and the mistake was on another.

The line in an error message is where the program noticed something was wrong. That’s often not where you made the mistake. In Python, it noticed on line 3, but the mistake was on line 2. In JavaScript, it noticed on line 4, but the mistake was on line 7.

So treat the reported line as the place to start. Ask: which value here is wrong? Then follow that value back to where it came from.

A missing bracket is the extreme case. Leave out a closing } in JavaScript, and the error may point at the very end of the file, because that’s where it finally gave up looking for it. If a SyntaxError points at a line that looks fine, check the lines above it.

Skip the lines that aren’t yours

Real lists of calls are long. The one Node printed for the example above had seven more lines, like these:

    at Module._compile (node:internal/modules/cjs/loader:1929:14)
    at Module.load (node:internal/modules/cjs/loader:1651:32)

These are Node’s own code, which loads and starts your file. Anything starting with node:internal, or with a path inside node_modules, is someone else’s code. Your mistake is almost certainly not there. Look for the first line with your own file name, and start from that.

Some common messages, translated

You’ll see the same few messages again and again. Once you know what they mean, most of them take seconds to fix.

Message What it usually means
Cannot read properties of undefined (reading 'x') The value before .x is undefined. Find out why.
ReferenceError: total is not defined There’s no variable called total here. Check the spelling, and where you created it.
TypeError: users.lenght is not a function You called something that isn’t a function. Here, a typo: it’s length, and it isn’t called with ().
NameError: name 'total' is not defined Python’s version of the ReferenceError above.
IndexError: list index out of range You asked for a position the list doesn’t have, like users[2] above.
KeyError: 'email' You asked a dictionary for a key it doesn’t have.

Sometimes the message really is a mystery. Search is fine then, but search for the right thing. Copy the type and the message, and take out anything that belongs only to you: your file names, paths and variable names. TypeError: unsupported operand type(s) for +: 'int' and 'str' finds thousands of useful answers. A whole traceback with C:\Users\you\Desktop\shop.py in it finds nothing.

An error is a gift

It’s tempting to wish your code would just stop showing errors. But the bugs that really waste an evening are the ones with no error at all: the program runs, does the wrong thing, and says nothing. I wrote about one of those, and how a library can catch it, in Teaching a library to say “Did you mean…?”.

An error message is the opposite. It stops right away and tells you what, where and how. That’s a lot of help for free.

Try it

Take a small program that works, and break it on purpose. Misspell a variable. Take an item that doesn’t exist from a list. Delete a bracket. Add a number to some text. Each time, before you look at the code, read the message and guess what you broke and where.

After a week of doing this on purpose, the red text stops being scary. It just becomes the first thing you read.