Skip to content
Nilay Kabariya

Entry No.077·Fixes·Updated ·3 min read

ReferenceError: process is not defined in Vite (dev vs build)

In Vite 8, process.env.X crashes in dev but is silently undefined in the build; NODE_ENV works in both. Tested in Chromium: import.meta.env, define, polyfills.

by Nilay#vite#nodeFIXED

The error, verbatim

Jump to the fix ↓
Uncaught ReferenceError: process is not defined
✓ Reproduced on Windows 11 Pro; Ubuntu on GitHub Actions

Tested on

Vite
8.3.4, 8.3.5
Plugin
vite-plugin-node-polyfills 0.28.0
Browser
Chromium (built-in browser; Playwright)
Node
24.14.0, 24.22.1
OS
Windows 11 Pro; Ubuntu on GitHub Actions

✓ Last re-run on GitHub Actions · see the run · all repros

Contents
  1. Your own code: use import.meta.env01
  2. A package that uses process: define it02
  3. How this was tested03

The same line of code can crash in Vite’s dev server and then “work” in the production build with an empty value. In my test on Vite 8.3.5, process.env.API_KEY threw process is not defined under vite, and came back undefined with no error after vite build, while process.env.NODE_ENV worked in both. The reason is that process belongs to Node: browsers don’t have it, older bundlers like webpack 4 quietly added a fake one, and Vite doesn’t.

What surprised me is that Vite’s dev server and its production build don’t fail the same way. I ran one small file through both on Vite 8.3.5 and read the results in Chromium, including a dependency that reads process:

Code in the browser vite (dev server) vite build + preview
process.env.API_KEY process is not defined no error, value is undefined
process.env.NODE_ENV ✅ "development" ✅ "production"
process.platform process is not defined process is not defined
a dependency’s process.env.NODE_ENV ✅ "development" ✅ "production"
a dependency’s process.env.DEBUG process is not defined no error, undefined

So process.env.SOMETHING can crash in development and then “work” in production with an empty value, or the other way round if you only test the build. Either way, the setting never reaches your code.

process.env.NODE_ENV is the exception: Vite replaces it with the mode, in your code and in dependencies, so that one never throws. If the error points at a line with NODE_ENV, look again: the crash is another process reference nearby.

Your own code: use import.meta.env

Vite exposes .env variables whose names start with VITE_ on import.meta.env. Rename the variable in .env:

VITE_API_KEY=your-value

and read it like this:

const key = import.meta.env.VITE_API_KEY;

A package that uses process: define it

When the process reference is in a dependency you can’t edit, tell Vite what to replace it with at build time:

// vite.config.js
export default {
  define: {
    'process.env.API_KEY': JSON.stringify('value'),
    'process.platform': JSON.stringify('browser'),
  },
};

Define exactly what the package reads. The values are pasted into your JavaScript as text, which means they’re visible to anyone who opens the page: I found defined-at-build in the built file.

The common shortcut: 'process.env': {}

Many answers suggest replacing the whole object:

// vite.config.js
export default { define: { 'process.env': {} } };

This stops the crash for anything under process.env: every one of those expressions became undefined in the dev server and in the build, and NODE_ENV still worked. But it doesn’t create process. process.platform still threw process is not defined in the build, even though it no longer threw in the dev server. If a package reads more than process.env, use a polyfill instead.

A package that needs more of Node: vite-plugin-node-polyfills

npm install -D vite-plugin-node-polyfills
// vite.config.js
import { nodePolyfills } from 'vite-plugin-node-polyfills';
export default { plugins: [nodePolyfills()] };

None of the setups put the non-VITE_ API_KEY from .env into the built files: I searched dist for its value and found nothing.

How this was tested

Windows 11 Pro, Node 24.14.0, Vite 8.3.4, October 18, 2026. A one-file app read process.env.API_KEY, process.platform and import.meta.env values from a .env file, and wrote what it got onto the page. Each version ran in vite (dev server) and vite build with vite preview, opened in Chromium (Claude’s built-in browser), before and after adding define. Every message above is the real output.

November 1, 2026: re-run on Vite 8.3.5 and Node 24.22.1 on GitHub Actions ubuntu-latest, read in Chromium through Playwright, adding process.env.NODE_ENV, a dependency that reads process, define: { 'process.env': {} } and vite-plugin-node-polyfills 0.28.0. The script and the full output are public.

— N.K., end of entry No.077

Useful? Pass it on:Post on XFollow @EmotionalMatter

Related entries

  1. No.001

    Fix: “Cannot use import statement outside a module”

    Why Node, Jest, TypeScript output or your browser refuses import (or throws Unexpected token 'export'), and the fix for each. Tested on Node 20 to 26.

    > SyntaxError: Cannot use import statement outside a module

    FIXED6 min
  2. No.080

    Fix: No 'Access-Control-Allow-Origin' header is present

    Blocked by CORS policy, but Postman and curl work fine. Five versions of the error reproduced in Chrome with Express and Vite, and which fix fits each.

    > Access to fetch at 'http://localhost:3001/plain/data' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

    FIXED3 min
  3. No.018

    "Cannot find native binding" in Vite 8: the missing @rolldown file

    Vite 8 fails with "Cannot find native binding" and names @rolldown/binding-wasm32-wasi? That's a fallback, not your missing package. Both causes, and the fix.

    > Error: Cannot find native binding. npm has a bug related to optional dependencies

    FIXED8 minWindows
  4. No.093

    Fix: bigint Failed to load bindings, pure JS will be used

    bigint: Failed to load bindings, pure JS will be used. Why npm 12 shows it on every install, whether it matters, and how to get the native build back.

    > bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)

    FIXED3 minWindows

Post card · Every Sunday

What broke this week.

One email on Sundays: the Node, TypeScript, Python and AI SDK releases that broke builds that week, each one reproduced, with a link to the fix that worked. Never shared, and you can leave any time.

— Nilay

or follow by RSSor on X

By subscribing you agree to the privacy note. One click to leave.

tip: paste the exact error text