Browsers throw Uncaught ReferenceError: require is not defined because require() is a CommonJS module-loading function built into runtimes like Node.js, not a native global function in standard browser JavaScript environments. When you load a script directly in a browser that calls require("...")—such as a Node script, an unbundled npm package, or a file written for CommonJS—the browser looks for a global require identifier and throws a ReferenceError.
1. Why require Exists in Node.js but Not in Browsers
Node.js provides require() at runtime to load local files or node_modules packages synchronously from the file system, whereas browsers execute scripts over HTTP/HTTPS and use native ES Modules (import / export) or a frontend build tool / bundler instead:
// CommonJS syntax (works in Node.js, throws ReferenceError in a raw browser script):
// const { format } = require("date-fns");
// Native browser ES Module syntax (when served properly with type="module"):
import { format } from "https://esm.sh/date-fns";
export function formatToday() {
return format(new Date(), "yyyy-MM-dd");
}
- Missing runtime global: Browsers have
window,document, andglobalThis, not Node’srequire,module, orexports. - File-system vs. web loading:
require()expects Node module resolution rules that do not exist natively in browser engines.
2. Fix 1: Bundle Your Code with a Frontend Build Tool
If your project imports npm packages or splits code across multiple local files, use a bundler or build tool (such as Vite, webpack, Parcel, or esbuild) so require() and package imports are resolved and compiled into browser-compatible assets:
// Typical application entry file (bundled by Vite / webpack / esbuild):
import { createRoot } from "react-dom/client";
function mountApp(container) {
if (!container) {
throw new Error("Target mount element not found.");
}
const root = createRoot(container);
return root;
}
// Build script example in package.json:
// "scripts": { "build": "vite build" }
- Best for real apps: Bundlers handle
node_modulesresolution, asset paths, environment variables, and browser compatibility automatically. - Avoid exposing
node_modulesraw: Pointing a<script src="/node_modules/...">tag directly at CommonJS files commonly triggers this error.
3. Fix 2: Convert to Native ES Modules (type="module") or Load a Browser Build
For lightweight scripts without a heavy bundler, switch from require() to ES module import syntax with <script type="module">, or include a prebuilt browser/UMD/ESM bundle via a CDN or build output:
// In HTML:
// <script type="module" src="./app.js"></script>
// Inside ./app.js (pure browser ES module, no require()):
export function buildApiUrl(baseUrl, path) {
if (typeof baseUrl !== "string" || typeof path !== "string") {
throw new TypeError("Expected baseUrl and path to be strings.");
}
return new URL(path, baseUrl).toString();
}
- Check relative paths: In native ES modules, relative local imports typically require explicit file extensions (such as
./helper.js). - Keep Node code on the server: If a file uses
fs,path, orprocess.envalongsiderequire(), move that logic to a backend endpoint or API instead of running it in the browser.
TL;DR: Browsers throw Uncaught ReferenceError: require is not defined because require() belongs to CommonJS (Node.js) rather than browser JavaScript—fix it by bundling your app (with Vite, webpack, esbuild, or Parcel) or migrating to native ES Modules (import/export with <script type="module">) and browser-compatible packages.
