Fix "ReferenceError: x is not defined" ($, require, process, window)
Uncaught ReferenceError: $ is not defined
- ReferenceError: require is not defined
- ReferenceError: process is not defined
- ReferenceError: window is not defined
- ReferenceError: document is not defined
- ReferenceError: Can't find variable: $
The name you use does not exist in any reachable scope: a variable never declared, a script not loaded yet (jQuery $), a Node.js global used in the browser (require, process) or a browser global run on the server (window, document). Declare or import the name, load the script first, or use the environment equivalent (import, import.meta.env).
What this error means
When JavaScript meets an identifier, it looks it up in the current scope, then in each parent scope up to the global object. If it finds it nowhere, it throws a ReferenceError, whereas a missing object property simply yields undefined. The most common case is code written for one environment running in another: require, module, process and Buffer only exist in Node.js, while window, document and localStorage only exist in the browser, and therefore not during Next.js or Nuxt server rendering. Safari words the same error "Can't find variable: x".
Common causes
- Scripts loaded in the wrong order: your code uses
$orjQuerybefore the jQuery<script>tag, or jQuery is loaded withdefer/asyncwhile your inline script is not. - Node.js global in the browser:
require(),module.exports,process.envorBufferin front-end code. Vite and webpack 5 no longer provide these globals automatically. - Browser global on the server:
window,documentorlocalStoragerun during server rendering (Next.js, Nuxt, SvelteKit) or in a Node.js script. - Variable not declared, misspelled or out of scope: declared with
let/constinside anifblock or a function, then used elsewhere. - Module script: in a
<script type="module">, top-level functions are not global, so anonclick="save()"in the HTML cannot find them.
Step-by-step fix
-
Identify the name and the environment
The name after "ReferenceError:" and the stack trace line tell you what to look for. Ask yourself where this code runs: browser, Node.js, or a framework server render.
-
"$ is not defined": load jQuery before your code
Put the jQuery tag before any script that uses it, without
async. In a bundled project, import it instead of relying on a global variable.<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <script src="/js/app.js"></script> <!-- with a bundler: npm install jquery, then in app.js --> <!-- import $ from 'jquery'; --> -
"require is not defined" in the browser: use
importThe browser does not understand CommonJS. Write ES modules and let Vite, webpack or esbuild bundle the code. In Node.js with
"type": "module", the message becomes "require is not defined in ES module scope": useimportthere too, or rename the file to.cjs.import axios from 'axios'; import { formatDate } from './utils.js'; -
"process is not defined": read environment variables on the front end
With Vite, variables exposed to the browser start with
VITE_and are read viaimport.meta.env. Never put secrets in them: they end up in the public JavaScript. With webpack 5, define the values withDefinePlugin.// .env // VITE_API_URL=https://api.example.com const apiUrl = import.meta.env.VITE_API_URL; -
"window is not defined": only touch the browser after mount
During server rendering,
windowdoes not exist. Move the code intouseEffect(React) oronMounted(Vue), or checktypeof window.typeofnever throws aReferenceError, even for a missing name.'use client'; import { useEffect, useState } from 'react'; export default function ScreenWidth() { const [width, setWidth] = useState(null); useEffect(() => { setWidth(window.innerWidth); }, []); return <p>{width ?? '...'}</p>; } // outside components const isBrowser = typeof window !== 'undefined'; -
Module scripts: attach events in JavaScript
Instead of an
onclickattribute that looks for a global function, wire the event inside the module. Exposing the function withwindow.save = savealso works, but pollutes the global scope.function save() { console.log('saved'); } document.querySelector('#save').addEventListener('click', save);
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Enable the ESLint rule
no-undefand declare the environment (globals.browserorglobals.nodeineslint.config.js): unknown names are flagged before runtime. - Keep server and client code clearly separated, and only use browser APIs in components or hooks that run on the client.
- Import your dependencies instead of relying on global variables loaded by
<script>tags.
Frequently asked questions
What is the difference between "x is not defined" and "x is undefined"?
"is not defined": the variable does not exist at all (ReferenceError). "undefined": the variable exists but has no value, and reading a property on it is what raises a TypeError.
What does "Cannot access 'x' before initialization" mean?
It is another ReferenceError: the variable is declared with let, const or class, but used before its declaration line (temporal dead zone), often because of a circular import. Move the declaration or break the import cycle.
Why "$ is not defined" only on some pages?
Those pages do not include jQuery, or include it after your script. Check the layout or template of those pages and the order of the <script> tags.
Useful tools for this error
More JavaScript & Node.js errors
- Fix "Cannot read properties of undefined" in JavaScript
- Fix "blocked by CORS policy: No 'Access-Control-Allow-Origin' header"
- Fix "Unexpected token '<', "<!DOCTYPE "... is not valid JSON"
- Fix "Error: Cannot find module" in Node.js
- Fix "npm ERR! ERESOLVE unable to resolve dependency tree"
- Fix "Hydration failed because the server rendered HTML didn’t match the client" (React, Next.js)
- Fix "Too many re-renders. React limits the number of renders" in React
- Fix "Error: listen EADDRINUSE: address already in use" in Node.js