ReferenceError: x is not defined
ReferenceError: x is not defined in JavaScript means javaScript could not find that name in any scope. A typo, a let/const used outside its block, or a browser-only global used on the server.
How do I fix ReferenceError: x is not defined?
ReferenceError: x is not defined means JavaScript cannot find that name in any scope. Common causes are a typo, a let or const used outside the { } block it was declared in, a missing import, or browser globals like window and document used on the server. Fix the spelling, move the declaration, or run browser code in useEffect.
- 1. Search for the declarationSearch the file for the name in the message. No declaration means a typo or missing import.
- 2. Check block scopeA let or const declared inside { } does not exist outside it. Declare it before the block.
- 3. Check where it runswindow, document and localStorage do not exist in Node or server rendering. require does not exist in ES modules.
What does ReferenceError: x is not defined mean?
Unlike reading a missing property (which gives undefined), using a name that was never declared throws. Common variants have very specific causes: "document is not defined" and "window is not defined" mean browser code ran in Node or during server-side rendering; "require is not defined" means CommonJS code ran as an ES module or in the browser.
Why does ReferenceError: x is not defined happen?
1. A variable declared inside a block
let and const exist only inside the { } where they were declared.
Breaks
if (user) {
const name = user.name;
}
console.log(name);Works
let name = "";
if (user) {
name = user.name;
}
console.log(name);2. document or window on the server
Next.js and other frameworks render on the server first, where there is no DOM.
Breaks
const width = window.innerWidth; // top level of a componentWorks
useEffect(() => {
setWidth(window.innerWidth); // runs in the browser only
}, []);3. require in an ES module
Files with "type": "module" or .mjs use import, and require does not exist.
Breaks
const fs = require("fs");Works
import fs from "fs";4. A typo
userName and username are different names.
Breaks
const userName = "ada";
console.log(username);Works
const userName = "ada";
console.log(userName);How to find it in your own code
Read the name in the message and search for its declaration. If there is none, it is a typo or a missing import. If it is inside { }, move the declaration out. If it is document, window or localStorage, the code is running somewhere without a browser — move it into an effect or event handler.
Try it: fix this bug
mediumfindFirstNegative([3, -2, 5]) throws ReferenceError: result is not defined.
function findFirstNegative(nums) {
for (let i = 0; i < nums.length; i++) {
if (nums[i] < 0) {
let result = nums[i];
}
}
return result;
}More challenges with this error
Each of these challenges runs into this error. Find the bug and fix it, no install and no account needed.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.