Home › JavaScript

ReferenceError: process is not defined (JavaScript) — how to fix

Uncaught ReferenceError: process is not defined
    at index.js:3:15

Quick fix: In Vite use import.meta.env.VITE_MY_VAR (variables must start with VITE_) instead of process.env.MY_VAR. In webpack 5 replace values at build time with new webpack.DefinePlugin({ 'process.env.API_URL': JSON.stringify(process.env.API_URL) }). For a dependency that needs process, add ProvidePlugin({ process: 'process/browser' }).

What it means

process is a Node.js global. Browsers don't have it. When you write process.env.API_URL in frontend code, a bundler must replace that expression with a literal string at build time; if it doesn't, the browser evaluates process and throws.

Fix 1: Vite - use import.meta.env

# .env
VITE_API_URL=https://api.example.com
const apiUrl = import.meta.env.VITE_API_URL;

Only variables prefixed with VITE_ are exposed to client code (others stay private). Built-ins: import.meta.env.MODE, import.meta.env.DEV, import.meta.env.PROD. Restart the dev server after editing .env.

When migrating a large CRA codebase, you can map the old names in vite.config.js:

import { defineConfig, loadEnv } from 'vite';

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), 'REACT_APP_');
  return {
    define: {
      'process.env.REACT_APP_API_URL': JSON.stringify(env.REACT_APP_API_URL),
    },
  };
});

Avoid define: { 'process.env': process.env }: it inlines every variable from your machine, including secrets, into the bundle.

Fix 2: webpack 5 - DefinePlugin

// webpack.config.js
const webpack = require('webpack');

module.exports = {
  plugins: [
    new webpack.DefinePlugin({
      'process.env.API_URL': JSON.stringify(process.env.API_URL),
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'),
    }),
  ],
};

DefinePlugin does text replacement, so the value must be JSON.stringify-ed.

Fix 3: a dependency uses process (e.g. process.nextTick, process.browser)

Install the browser shim and inject it:

npm install process
new webpack.ProvidePlugin({ process: 'process/browser' })

In Vite, prefer a browser build of the library; otherwise vite-plugin-node-polyfills can provide process, Buffer and friends.

Fix 4: Next.js

Next.js does inline process.env.X in client code, but only for variables prefixed with NEXT_PUBLIC_, and only when written literally. Dynamic access like process.env[name] or destructuring const { API } = process.env is not replaced and fails in the browser. Write process.env.NEXT_PUBLIC_API_URL directly.

Fix 5: plain <script> without a bundler

There's no build step to replace anything. Put config in a global or a fetched JSON file:

<script>window.APP_CONFIG = { apiUrl: "https://api.example.com" };</script>

If the error is about require or module instead, see require is not defined; for window on the server, see window is not defined.

Last updated 2026-11-24.