You asked AI to build a login form. It generated 80 lines of clean-looking React code. You pasted it in, hit save, and your browser console exploded in red. Your instinct is to copy the entire error back into the AI chat and say "fix this." Sometimes that works. But when it doesn't, and it often doesn't, you need to read the error yourself. The good news: error messages follow a predictable structure, and once you learn to read them, most bugs reveal themselves in under 30 seconds.
Error messages are structured, not random
Every JavaScript error follows the same anatomy. Here's a real one you might see after AI generates a component:
Uncaught TypeError: Cannot read properties of undefined (reading 'map')
at ProductList (ProductList.jsx:12:24)
at renderWithHooks (react-dom.development.js:14985:18)
at mountIndeterminateComponent (react-dom.development.js:17811:13)This looks intimidating, but it breaks down into three clear pieces:
| Part | Example | What it tells you |
|---|---|---|
| Error type | TypeError | The category of problem, you tried to use a value in a way its type doesn't support |
| Error message | Cannot read properties of undefined (reading 'map') | Plain English explanation: something is undefined and you're calling .map() on it |
| Stack trace | at ProductList (ProductList.jsx:12:24) | Exact file, line number, and column where the crash happened |
The error type and message together are usually enough to understand the bug. The stack traceWhat is stack trace?A list of function calls recorded at the moment an error occurs, showing exactly which functions were active and in what order. tells you where to look.
How to read a stack traceWhat is stack trace?A list of function calls recorded at the moment an error occurs, showing exactly which functions were active and in what order.
A stack trace is a breadcrumb trail of function calls. In JavaScript, read it top-to-bottom: the top line is where the error actually happened, and each line below is the function that called it.
at ProductList (ProductList.jsx:12:24) ← crash happened here
at renderWithHooks (react-dom.development.js:14985:18) ← React called your component
at mountIndeterminateComponent (react-dom.development.js:17811:13) ← React mounting systemThe format ProductList.jsx:12:24 means file ProductList.jsx, line 12, column 24. In your browser devtools, you can click this link to jump directly to that line.
Filtering out framework noise
Most of the stack trace is framework internals, React, Next.js, Webpack. You can safely ignore any line referencing files you didn't write. Focus on the lines that reference your own files.
at ProductList (ProductList.jsx:12:24) ← YOUR CODE - start here
at App (App.jsx:28:5) ← YOUR CODE - called from here
at renderWithHooks (react-dom.development.js:14985:18) ← ignore
at mountIndeterminateComponent (react-dom.development.js:17811:13) ← ignoreCommon errors in AI-generated code
AI generates certain errors far more often than others. Learning these patterns means you can often diagnose the bug just from reading the error type.
TypeError: Cannot read properties of undefined
This is the single most common error in AI-generated code. It happens when AI assumes data exists that hasn't loaded yet:
// AI generates this:
function UserProfile({ user }) {
return <h1>{user.name}</h1>;
}
// But user is undefined on first render because the API
// hasn't responded yet. Crash.The fix is almost always adding a guard:
function UserProfile({ user }) {
if (!user) return <p>Loading...</p>;
return <h1>{user.name}</h1>;
}ReferenceError: X is not defined
AI used a variable or function that doesn't exist in scopeWhat is scope?The area of your code where a variable is accessible; variables declared inside a function or block are invisible outside it.. This often happens when AI generates code that references a utility function it forgot to define, or imports from a package you haven't installed:
ReferenceError: formatDate is not definedCheck: did the AI define formatDate somewhere? Did it forget an import? Is it a function from a library you don't have?
SyntaxError
The code itself is malformed. AI occasionally generates incomplete code, missing closing brackets, dangling commas, or mixing up JSXWhat is jsx?A syntax that lets you write HTML-like markup inside JavaScript, which React converts into actual DOM elements. and regular JavaScript:
SyntaxError: Unexpected token '<'This usually means JSX is appearing in a file that isn't configured to handle it, or the AI truncated its output mid-line.
TypeError: X is not a function
AI called something as a function that isn't one. This often happens with APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses. mismatches, the AI thinks a library exports a function, but the version you have doesn't include it:
// AI writes:
import { useSearchParams } from 'react-router-dom';
const [params, setParams] = useSearchParams();
// But your version of react-router-dom doesn't export useSearchParams
// TypeError: useSearchParams is not a function| Error type | Most likely AI cause | First thing to check |
|---|---|---|
| TypeError: Cannot read properties of undefined | Data not loaded yet, or wrong object shape | Is the variable actually defined at that point? |
| ReferenceError: X is not defined | Missing import or undefined helper function | Search the file, is X actually declared? |
| SyntaxError | Truncated output, mixed syntax | Look at the exact line, is something obviously incomplete? |
| TypeError: X is not a function | Wrong library version or API | Check the library docs for your installed version |
| Module not found | AI referenced a package you don't have | Run npm install for the missing package |
Compile-time vs runtimeWhat is runtime?The environment that runs your code after it's written. Some languages need a runtime installed on the machine; others (like Go) bake it into the binary. errors
This distinction matters because it changes when and where you'll see the error.
Compile-time errors appear in your terminalWhat is terminal?A text-based interface where you type commands to interact with your computer. Also called the command line or shell. or editor before the code runs. TypeScript errors, ESLint errors, and build-tool errors are all compile-time. They prevent your code from even starting:
error TS2339: Property 'naem' does not exist on type 'User'.
Did you mean 'name'?Runtime errors happen while your app is running, when a user clicks a button, when data loads from an APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses., when a specific code path executes. You see these in the browser console:
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')A practical debugging sequence
When AI-generated code throws an error, follow this sequence instead of immediately asking AI to fix it:
- Read the error type and message: what category of bug is this?
- Look at the first stack traceWhat is stack trace?A list of function calls recorded at the moment an error occurs, showing exactly which functions were active and in what order. line referencing your code: which file and line?
- Open that file and look at that exact line: what's happening there?
- Check if it matches a common AI pattern: undefined data? Missing import? Wrong APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses.?
- If you can see the fix, make it. If not, now ask AI, but include the specific line, the error, and what you observed.
This takes 30 seconds and saves you from the "AI fix loop" where AI makes the problem worse because it doesn't understand the context.