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.
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.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:
formatValueVerify: You can now say whether the name is undeclared, out of scope, missing an import, or expected from a library that did not load.
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
JavaScript identifiers are case-sensitive, so
userData and userdata are different names.Values declared with
let or const inside a block are not visible outside that block.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';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.
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>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.
What not to do
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.
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.
Loading community discussion…
Comments could not load here. Open ErrorHarbor Discussions on GitHub →