Skip to content

JavaScript and build errorReferenceError: “process is not defined”

A page goes blank and the console shows Uncaught ReferenceError: process is not defined. process is a Node.js global. Browsers do not have it, and only some bundlers replace process.env values in your code when they build it. Vite does not define process.env.YOUR_VAR for browser code, and webpack 5 stopped adding Node.js shims for you.

Short answer

In a Vite app, read env variables from import.meta.env, and name the ones the browser needs with the VITE_ prefix. In Next.js, browser code can only read variables named NEXT_PUBLIC_, which are inlined at build time. With webpack 5, replace the values you use with DefinePlugin. If the error comes from a library inside node_modules, it was written for Node.js: look for its browser build, or define the one value it reads.

Why process is missing, and the fix

  1. 01

    process.env in a Vite app

    “Variables prefixed with VITE_ will be exposed in client-side source code after Vite bundling.” Vite, Env variables and modes

    Vite exposes env variables on import.meta.env, not process.env, and only those prefixed with VITE_ reach browser code. Anything else is kept out of the browser bundle, which also keeps secrets out of it.

    # .env
    VITE_API_URL=https://api.example.com
    
    // src/api.js
    const baseURL = import.meta.env.VITE_API_URL;
  2. 02

    webpack 5 without the old Node.js shims

    “Webpack 5 no longer polyfills Node.js core modules automatically” webpack, Resolve: resolve.fallback

    webpack 4 quietly added a fake process object. webpack 5 does not, so code that reads process.env breaks after an upgrade. Replace the exact values at build time with DefinePlugin.

    const webpack = require('webpack');
    module.exports = {
      plugins: [
        new webpack.DefinePlugin({
          'process.env.API_URL': JSON.stringify(process.env.API_URL),
        }),
      ],
    };
  3. 03

    Next.js variables without NEXT_PUBLIC_

    In Next.js, process.env.SECRET works in server code but is undefined in client components, and dynamic access such as process.env[name] is never inlined. Prefix browser variables with NEXT_PUBLIC_ and read each one by its full name.

    const url = process.env.NEXT_PUBLIC_SUPABASE_URL; // inlined at build time
  4. 04

    A library written for Node.js

    When the stack trace points into node_modules, a package expects Node.js globals. Check for a browser or ESM build in its docs, or define the value it reads in your bundler config. Avoid adding a full process polyfill just to silence it.

Checked against the vendor’s docs on : Vite: Env variables and modes; webpack: Resolve (resolve.fallback).

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: Vercel environment variables undefined · “require is not defined” · Firebase “invalid API key” in production

Questions

More on this error

Can I just add define: { process: {} } to make it go away?

It hides the error, but every value read from process.env becomes undefined. Define the specific values you need instead.

Is it safe to expose env variables to the browser?

Only values meant to be public, such as a public API URL or a publishable key. Anything in the browser bundle can be read by anyone, so keep secrets in server code.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. 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.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.