Skip to main content

Lesson 7 of 10 · Own the code AI writes

How to read error messages and debug your code

How to read error messages and stack traces, where to find them, the common errors in plain English, and how to debug without going round in circles.

When something breaks, the quickest move is to copy the error, paste it into the AI and say "fix this". Sometimes that works. Often you get a change that makes the error go away without fixing what caused it, and a few changes later you're going round in circles with an app that's worse than when you started.

An error message isn't an obstacle. It's the most useful thing the app has said to you all day. This lesson is about reading it.

Where to find error messages

The same app can report errors in four places, and the one you need depends on where the code was running:

  • The browser's console. Errors from code running in the visitor's browser. Open the developer tools (F12 on Windows, Cmd+Option+I on a Mac) and choose Console.
  • The browser's Network tab. Every request the page made, with its status. A request in red failed. Click it to see what the server sent back.
  • The terminal. Errors from code running on the server while you develop: Server Components, API routes, anything in npm run dev. If the browser says something went wrong and the console's empty, look here.
  • Your host's logs. Once the site's live, server errors go to your host's logs: on Vercel, the Logs tab of the project. Nothing tells you they're there. You have to look.

Next.js also shows an error overlay in the browser while you're developing. It's showing you the same message: read it the same way.

How to read an error message and a stack trace

Here's one you'll meet sooner or later:

TypeError: Cannot read properties of undefined (reading 'map')
    at OrderList (app/orders/order-list.tsx:12:23)
    at renderWithHooks (node_modules/react-dom/...)
    at ...

It has three parts.

The type: TypeError. The code tried to do something with a value of the wrong kind.

The message: Cannot read properties of undefined (reading 'map'). The code tried to use .map on something, and that something was undefined, meaning it had no value at all. You met .map in lesson 3: it goes through a list. So something that should have been a list wasn't there.

The stack trace: the list of lines underneath, showing which function called which when it broke. Read down until you reach the first file that's yours, not something in node_modules. Here that's app/orders/order-list.tsx:12:23: the file, line 12, character 23. Open it. That's where it broke.

Where it broke isn't always where it went wrong. Line 12 might be orders.map(...), and the real question is why orders was missing. Did the data not load yet? Did the server send something with a different shape? Did a name change? Follow the value back to where it came from.

The fix that isn't a fix

Ask an AI to fix that error and there's a good chance it changes the line to this:

{orders?.map((order) => ( ... ))}

The ?. means "only if it exists". The error's gone. So are the orders: the page now shows nothing, quietly, and nobody's told. You've swapped a loud problem for a silent one.

Sometimes ?. is the right answer, when it's normal for the value to be missing for a moment while the data loads. Then the page should say "Loading" or "No orders yet", not show a blank space. Either way, the question is why the value was missing, and that's what to ask the AI: "Why is orders undefined here? Don't change the code yet."

Common errors in plain English

Cannot read properties of undefined (reading 'x')

Something you expected to have a value didn't. Find where that value comes from.

x is not defined

The code uses a name that doesn't exist where it's used: a typo, or something used without being imported.

x is not a function

The code tried to call something that isn't a function: a typo, an import that brought in the wrong thing, or a function an AI made up (lesson 5).

Module not found: Can't resolve 'x'

The code imports something that isn't there. Either the package isn't installed (run the install command, after checking the package is real) or the path to a file is wrong.

Hydration failed because the server rendered HTML didn't match the client

The wording varies a little between versions. Next.js and React drew the page twice, once on the server and once in the browser, and got different results. Common causes are dates and times, random numbers, and code that checks for window. Find what's different between the two.

Each child in a list should have a unique "key" prop

It shows in red, but it won't break the page. A list made with .map needs a key on each item, as in lesson 3. Use something unique, like an ID.

Access to fetch at '...' from origin '...' has been blocked by CORS policy

The browser stopped your page calling another website's address, because that site doesn't allow it. The fix is usually to make the call from your server instead of the browser. Don't let an AI "fix" it by turning the protection off on a server you own without understanding what that allows.

EADDRINUSE: address already in use :::3000

Something's already running on that port, usually another copy of your dev server. Close the other terminal, or stop it. Some tools, Next.js's dev server among them, pick the next port instead and say so in the terminal, so check which address you're actually looking at.

Status codes in the Network tab

401: not logged in. 403: logged in, not allowed. 404: nothing at that address. 500: the server crashed. For a 500, the browser won't tell you why. The terminal or your host's logs will.

How to debug without going round in circles

This is the method I'd teach anyone. It works with or without AI.

  1. Read the whole error. All of it, slowly, including the first line of the stack trace that's yours.
  2. Make it happen again. Find the exact steps that cause it. An error you can repeat is half fixed.
  3. Guess, in one sentence. "I think orders isn't a list: the API returns { data: [...] } and the page expects the list itself."
  4. Check the guess. Add a console.log before the line that breaks and look at the real value. Or look at the response in the Network tab.
  5. Change one thing. Then try again. If it didn't work, undo it before you try the next idea. Five changes stacked up and you won't know which one did what.
  6. Commit when it works. Lesson 8 covers this. It's what makes step 5 safe.

How to give an error to an AI

When you do ask the AI, give it what a person would need:

I expected: the orders page to list the logged-in customer's orders.
What happened: the page crashes with the error below.
Steps: log in as a test customer, go to /orders.
What I've checked: the API returns 200 and the response looks like
{ data: [...] }.

ERROR AND STACK TRACE HERE

Explain the cause first. Don't change any code until I've agreed.

That last line matters. It turns the AI from something that guesses at fixes into something that helps you understand, and the fix it suggests afterwards is far more likely to be the right one.

Stop after three tries. If the AI's had three goes at the same error and it's still there, or a new one's appeared, stop. Undo back to the last version that worked, read the error again yourself, and start from step 1. Going round a fourth time rarely helps.

If you built something with AI

What to check

  • Open your live site with the developer tools open, click through every page, and note any red in the Console or the Network tab.
  • Find your host's logs and look at the last week. Are there errors you didn't know about?
  • Search your project for ?. next to .map and check each one: when the list is missing, does the page tell the visitor something, or show a blank?
  • Make sure you'd find out if your live site broke: an error alert from your host, or an uptime check.

If you're learning the trade

Try it

Take the app you've been working on and break it on purpose, three ways, one at a time:

  1. Misspell the name of a variable inside a component.
  2. Change an import to a package that doesn't exist.
  3. Make an API route return { data: orders } where the page expects a plain list. You should get orders.map is not a function. If your app has no such route, ask an AI for an /api/orders route that returns three made-up orders, and a page that lists them, then break that.

For each one, before you look anywhere else: find the error, read it, name the file and line, and write down the cause in one sentence. Then fix it yourself. Only then ask an AI what the error meant, and compare.

Want a hand with your own code?

One to one, we go through what you built together and decide what to keep.