JavaScript ReferenceError: x is not defined — Check Name, Scope and Script Order

ReferenceError: x is not defined means JavaScript tried to read an identifier that does not exist in the current execution context. Start with the exact name and scope before changing packages or build tools.

JavaScriptBrowsers · Node.jsLast reviewed Sep 25, 2026
🎯 You’re likely in the right place if:
The console shows ReferenceError: ... is not defined.The name is a variable, function, imported symbol or library global.The error appears when code first tries to read that identifier.
Choose the path that matches what you see
Name was never declaredDeclare it or import it in the scope where the failing line runs.
Name exists elsewhereCheck block/function/module scope and the first stack-trace frame.
Name comes from a libraryConfirm the library request succeeds and loads before your code uses its global.
Message says “before initialization”That is a temporal-dead-zone case; move the access after the let/const initialization instead of treating it as a missing global.
Difficulty: Easy–ModerateRisk: Low first
🔎 Quick Check — start from the first stack-trace frame

Open the first stack frame that belongs to your code and identify the exact unresolved name on that line. Then search for its declaration or import in the same module and enclosing scopes.

// Example error ReferenceError: formatValue is not defined // Inspect the first frame in your code, then search for: formatValue

Verify: You can now say whether the name is undeclared, out of scope, missing an import, or expected from a library that did not load.

Did this reveal the missing name or scope?
ADSENSE · reserved slot after the first useful step

Why this happens

MDN documents this error as a missing identifier reference. The name may never have been declared, may be outside the current block/function/module scope, or may belong to a library script that has not loaded yet.

Diagnose before changing more

1
Is the spelling identical?
JavaScript identifiers are case-sensitive, so userData and userdata are different names.
2
Was the name declared inside another scope?
Values declared with let or const inside a block are not visible outside that block.
3
Is it provided by another script or module?
Confirm that the import exists or that the dependency script loads before code that uses its global.

Fix #2 — declare or import the identifier in the correct scope

If the name belongs to your code, declare it before use or import/export it through the module boundary that needs it. Avoid creating accidental globals just to silence the error.

// module-a.js export function formatValue(v) { return String(v); } // module-b.js import { formatValue } from './module-a.js';
🟡 Before changing configuration: Keep the declaration as narrow as practical. Moving everything to global scope can hide the real architecture problem.

Verify: Reload or rerun the exact failing path. The original ReferenceError should disappear; if a different error appears, follow that new error instead of continuing to change scope.

Did Fix #2 work?

Fix #3 — fix script or dependency load order

If the identifier comes from a library global, make sure the library script succeeds and is loaded before the script that references it. In module builds, prefer explicit imports over assuming a global exists.

<script src="library.js"></script>\n<script src="app.js"></script>
🟡 Keep it reversible: Check the Network and Console panels for a failed library request before changing application code.

Verify: Reload the page with DevTools Network open. The dependency request should succeed, and the first stack frame should no longer report the missing global.

Did Fix #3 work?

What not to do

Do not create a new global variable just to silence the error unless the application intentionally uses that global API.Do not reorder unrelated scripts without confirming which dependency provides the missing name.Do not confuse this with “Cannot access X before initialization”; that message points to let/const initialization order, not a missing identifier.

Still seeing the error?

If the name exists but the error remains, inspect the exact execution path: async loading, conditional imports, bundler aliases and code splitting can make a symbol unavailable only on certain routes or timing windows.

Before escalating, capture:the exact ReferenceError text and first stack framethe declaration/import you expected to provide the namewhether the failure occurs in browser, Node.js, or only after bundlingthe smallest reproducible file or route

Official references

Still stuck? Ask the community

Share your operating system, software version, exact error text, and which fix you already tried. Another reader may have seen the same setup.

Keep it safe: never post passwords, API keys, access tokens, recovery codes, license keys, private URLs, or confidential logs.
Powered by GitHub DiscussionsSign in with GitHub to comment. Reading comments does not require sign-in.

Loading community discussion…