ReferenceError: x is not defined
Code refers to a variable or function name that doesn’t exist in the current scope.
is not defined$ is not definedrequire is not definedwindow is not defineddocument is not defined Meaning
Unlike undefined (declared but no value), “not defined” means the identifier was never declared where the code runs. Classic sources: typos, missing imports, scripts loading in the wrong order, and Node-only globals used in the browser (or vice versa).
Common causes
- Typo or wrong case (
myVarvsmyvar) - Missing import / script tag
- Using a variable outside its block scope (
let/const) - Script order: library (e.g. jQuery
$) loaded after the code using it require,module,__dirnameused in browser or ES moduleswindow/documentused during server-side rendering (Next.js)- Accessing
let/constbefore declaration (temporal dead zone)
⚡ Quick fix
- Check spelling and scope at the line in the stack trace
- Add the missing import or
<script>before your code - In ES modules, replace
__dirnamewithpath.dirname(fileURLToPath(import.meta.url)) - Guard browser globals in SSR:
if (typeof window !== 'undefined')
Detailed fix by platform
Node.js
- __dirname in ES modules:javascript
import { fileURLToPath } from 'node:url'; import path from 'node:path'; const __dirname = path.dirname(fileURLToPath(import.meta.url));
React
- Next.js: move
window/localStorageaccess intouseEffect, which only runs in the browser.
How to diagnose
- Name — Spelled and cased correctly?
- Scope — Declared in an enclosing scope?
- Load order — Is the defining script/import loaded first?
- Environment — Browser vs Node vs SSR?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in JavaScript
- Failed to fetch TypeError: Failed to fetch / NetworkError when attempting to fetch resource
- QuotaExceededError QuotaExceededError: Failed to execute 'setItem' on 'Storage': Setting the value exceeded the quota
- module MIME type Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html"
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 14 Oct 2026