Short answer
Check the exact spelling and letter case of the name first. Then check where it was declared: let and const only exist inside the block where they are declared, so a variable created inside an if or a function is not visible outside it. For library globals such as $ or gtag, make sure the script that defines them loads before your code. For require, process, window or document, the code is running in an environment that does not have them; see the related fixes below.
Why the name does not exist here, and the fix
01
A typo or different letter case
“There is a non-existent variable referenced somewhere. This variable needs to be declared, or you need to make sure it is available in your current script or scope.” MDN, ReferenceError: "x" is not defined
userId and userID are different names. Search the file for the exact spelling, and let your editor’s rename tool change every use at once.
02
Declared in a different scope
A variable declared with let or const inside a block, loop or function does not exist outside it. Declare it in the scope where both the assignment and the use can see it.
// wrong if (loggedIn) { const name = user.name; } console.log(name); // ReferenceError // right let name = 'Guest'; if (loggedIn) { name = user.name; }03
A library script that has not loaded yet
“When loading a library (such as jQuery), make sure it is loaded before you access library variables, such as "$".” MDN, ReferenceError: "x" is not defined
Globals such as $, gtag or Stripe exist only after their script runs. A script tag placed after yours, or loaded with async, may not have run yet. Load it first, or wait for its load event.
<script src="https://js.stripe.com/v3/"></script> <script src="/js/checkout.js" defer></script>04
An undeclared assignment in a module or strict mode
Old scripts could write total = 0 without declaring it. ES modules and strict mode forbid that, so the same line now throws. Add let or const.
let total = 0;
Checked against the vendor’s docs on : MDN: ReferenceError: "x" is not defined.
Rather hand it over?
Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.See scope and start
Related: “require is not defined” · “process is not defined” · Next.js “window is not defined”