The fifteen errors every beginner hits
Collected from the modules so far and from a decade of Stack Overflow. Each card is the real message, the reason, and the fix. Read them once now; you will recognise them for the rest of your career. The first four account for most of the total.
1. TypeError: Cannot read properties of undefined (reading 'x')
const res = { data: null }
console.log(res.data.items.length)Uncaught TypeError: Cannot read properties of null (reading 'items')
at your code:2The value before .items is null/undefined. The property named in the message is the one you tried to read; the thing that was missing is whatever came before it. Causes: data not loaded yet, a typo in an earlier key, an API that returned less than you expected, an array index past the end.
Log the object one level up. Guard with ?. when absence is legitimate; fix the upstream data when it is not.
2. TypeError: x is not a function
const user = { name: "Ada" }
user.getName()
const items = "a,b"
items.map(x => x)Uncaught TypeError: user.getName is not a function
at your code:2You called something that is not callable: a method that does not exist on that object (typo, wrong type — a string has no map), a value that is undefined because an import failed, or a property you forgot to invoke earlier so you got the function and then called its result.
Check typeof user.getName. Check the import (default vs named). Check the type of the receiver — Array.isArray(items).
3. ReferenceError: x is not defined
function total(items) { return items.reduce((s, i) => s + i.price, 0) }
console.log(totl(cart))Uncaught ReferenceError: totl is not defined
at your code:2No variable with that exact name exists in scope: a typo, a variable declared inside a block or function you are now outside of, a missing import, or a browser global used in Node (window, document).
Match the spelling and case. Move the declaration up a scope or pass the value in. Add the import.
4. SyntaxError: Unexpected token
const config = {
port: 3000
host: "localhost",
}SyntaxError: Unexpected token ':' (or: missing ) after argument list / Unexpected end of input)The file could not be parsed, so nothing ran. Missing comma, bracket or quote — usually one line above where the error points, because the parser only notices when it reads the next token.
Use an editor with bracket matching and a formatter; the red squiggle is on the wrong line, the fix is above it. Unexpected end of input = an unclosed { or (.
5. TypeError: Assignment to constant variable
const count = 0
count++Uncaught TypeError: Assignment to constant variable.
at your code:2You reassigned a const. Mutating an object's contents is fine; rebinding the name is not.
let if it changes. If you meant to change a property, you are not hitting this error.
6. ReferenceError: Cannot access 'x' before initialization
console.log(api)
const api = createApi()Uncaught ReferenceError: Cannot access 'api' before initialization
at your code:1A let/const used above its declaration — the temporal dead zone. Often: a const arrow function called before the line that defines it, or two modules importing each other (circular import).
Move the use below the declaration, or make it a function declaration (hoisted). For circular imports, move the shared piece to a third module.
7. RangeError: Maximum call stack size exceeded
function getParent(node) { return getParent(node.parent) }
getParent(leaf)Uncaught RangeError: Maximum call stack size exceeded
at getParent (your code:1)
at getParent (your code:1)Infinite recursion — no base case, or a step that does not shrink the input. Also: a getter that reads itself (get x() { return this.x }), or an event handler that triggers its own event.
Add the base case first. For the getter, use a different backing name (#x).
8. Uncaught (in promise) / UnhandledPromiseRejection
fetchUser(id).then(user => render(user))
// fetchUser rejects; no .catch, no await in a tryUncaught (in promise) Error: HTTP 500
at fetchUser (api.js:12)A promise rejected and nothing handled it. In Node 15+ the process exits. The stack points at where it was thrown, not at the caller that forgot to catch.
Every promise gets an owner: await inside try/catch, or .catch(). Fire-and-forget calls get an explicit .catch(log).
9. Promise { <pending> } / undefined from an async function
async function load() { return await api.get("/x") }
const data = load()
console.log(data.items)undefined // and console.log(data) shows Promise { <pending> }Missing await: data is the promise. Also appears as "x.map is not a function" or "Cannot read properties of undefined" one line later.
const data = await load(), inside an async function or a module (top-level await).
10. SyntaxError: Cannot use import statement outside a module
import fs from "node:fs"SyntaxError: Cannot use import statement outside a moduleNode treated the file as CommonJS, or the browser script tag lacks type="module".
"type": "module" in package.json / .mjs extension / <script type="module">.
11. Error: Cannot find module './utils'
import { slug } from "./utils"Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/src/utils' imported from /app/src/index.jsESM needs the extension (./utils.js); the path is relative to the importing file, not the project root; or the package is not installed (npm install). On Linux servers, file names are case-sensitive — Utils.js ≠ utils.js, which works on a Mac and breaks in CI.
Add .js. Check the relative path from this file. Match the case exactly.
12. CORS: No 'Access-Control-Allow-Origin' header is present
fetch("https://api.other.com/data") // from https://myapp.comAccess to fetch at 'https://api.other.com/data' from origin 'https://myapp.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.The browser blocks reading cross-origin responses unless the server opts in. Your code is fine; the other server did not send the header. Nothing you do client-side can fix it — that is the point.
Own the API? Add CORS headers (or the cors middleware). Don't? Call it from your own backend and proxy the result.
13. [1, 2, 10].sort() → [1, 10, 2] and 0.1 + 0.2 !== 0.3
console.log([1, 2, 10].sort())
console.log(0.1 + 0.2 === 0.3, (0.1 + 0.2).toFixed(2))[ 1, 10, 2 ]
false 0.30Not errors — wrong answers. Default sort compares strings; binary floats cannot represent 0.1.
sort((a, b) => a - b). Compare floats with a tolerance; do money in integer cents.
14. TypeError: Converting circular structure to JSON
const a = { name: "a" }
a.self = a
JSON.stringify(a)Uncaught TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
--- property 'self' closes the circleThe object refers to itself (directly or via a parent/child link — DOM nodes, ORM models, Express req). JSON has no way to express that.
Stringify a plain view of the data: pick the fields you need, or pass a replacer function. Never stringify framework objects.
15. Warning: Each child in a list should have a unique "key" prop / Hooks called conditionally
// React
items.map(item => <li>{item.name}</li>)
if (open) { const [x, setX] = useState() }Warning: Each child in a list should have a unique "key" prop.
Error: Rendered more hooks than during the previous render.Framework rules, not language rules: React identifies list items by key, and hooks must run in the same order every render. Every framework has a handful of these — read its "rules" page once.
<li key={item.id}>. Call hooks at the top level, unconditionally; put the condition inside.
