Skip to content
  1. LuzineDebug
  2. JavaScript & Node.js
  3. Uncaught ReferenceError: $ is not defined
JavaScript & Node.js Beginner ≈ 10 min to fix

Fix "ReferenceError: x is not defined" ($, require, process, window)

Error message
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: $
In short

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

Be the first to confirm this fix

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

  1. Scripts loaded in the wrong order: your code uses $ or jQuery before the jQuery <script> tag, or jQuery is loaded with defer/async while your inline script is not.
  2. Node.js global in the browser: require(), module.exports, process.env or Buffer in front-end code. Vite and webpack 5 no longer provide these globals automatically.
  3. Browser global on the server: window, document or localStorage run during server rendering (Next.js, Nuxt, SvelteKit) or in a Node.js script.
  4. Variable not declared, misspelled or out of scope: declared with let/const inside an if block or a function, then used elsewhere.
  5. Module script: in a <script type="module">, top-level functions are not global, so an onclick="save()" in the HTML cannot find them.

Step-by-step fix

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

  2. "$ 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.

    xml
    <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'; -->
  3. "require is not defined" in the browser: use import

    The 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": use import there too, or rename the file to .cjs.

    javascript
    import axios from 'axios';
    import { formatDate } from './utils.js';
  4. "process is not defined": read environment variables on the front end

    With Vite, variables exposed to the browser start with VITE_ and are read via import.meta.env. Never put secrets in them: they end up in the public JavaScript. With webpack 5, define the values with DefinePlugin.

    javascript
    // .env
    // VITE_API_URL=https://api.example.com
    
    const apiUrl = import.meta.env.VITE_API_URL;
  5. "window is not defined": only touch the browser after mount

    During server rendering, window does not exist. Move the code into useEffect (React) or onMounted (Vue), or check typeof window. typeof never throws a ReferenceError, even for a missing name.

    javascript
    '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';
  6. Module scripts: attach events in JavaScript

    Instead of an onclick attribute that looks for a global function, wire the event inside the module. Exposing the function with window.save = save also works, but pollutes the global scope.

    javascript
    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-undef and declare the environment (globals.browser or globals.node in eslint.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

All JavaScript & Node.js errors →