ReferenceError JavaScript Origin: On your device Last updated 14 Oct 2026

ReferenceError: x is not defined

Code refers to a variable or function name that doesn’t exist in the current scope.

Exact message you may see is not defined$ is not definedrequire is not definedwindow is not defineddocument is not defined
Seen on: JavaScript Node.js React

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 (myVar vs myvar)
  • 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, __dirname used in browser or ES modules
  • window/document used during server-side rendering (Next.js)
  • Accessing let/const before declaration (temporal dead zone)

⚡ Quick fix

  1. Check spelling and scope at the line in the stack trace
  2. Add the missing import or <script> before your code
  3. In ES modules, replace __dirname with path.dirname(fileURLToPath(import.meta.url))
  4. Guard browser globals in SSR: if (typeof window !== 'undefined')

Detailed fix by platform

Node.js

  1. __dirname in ES modules:
    javascript
    import { fileURLToPath } from 'node:url';
    import path from 'node:path';
    const __dirname = path.dirname(fileURLToPath(import.meta.url));

React

  1. Next.js: move window/localStorage access into useEffect, which only runs in the browser.

How to diagnose

  1. Name — Spelled and cased correctly?
  2. Scope — Declared in an enclosing scope?
  3. Load order — Is the defining script/import loaded first?
  4. 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:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

$ is not defined — Console: Uncaught ReferenceError: $ is not defined (jQuery not loaded or loaded after your code) (Browser)$ is not definedalready declared — SyntaxError: Identifier 'x' has already been declared (JavaScript)already declaredCS0103 — error CS0103: The name 'x' does not exist in the current context (.NET)CS0103Hydration failed — Error: Hydration failed because the initial UI does not match what was rendered on the server (React)Hydration failedNameError — NameError: name 'x' is not defined (Python)NameErrorProperty was accessed during render but is not defined — Vue: [Vue warn]: Property "x" was accessed during render but is not defined on instance (Vue)Property was accessed…React must be in scope — ReferenceError: React is not defined / 'React' must be in scope when using JSX (React)React must be in scopeSyntaxError import — SyntaxError: Cannot use import statement outside a module (Node.js)SyntaxError importReference…

Explore the map of this error Full error map →

🧠 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.