Skip to content
Latchkey

Vite "process is not defined" in the browser build in CI

Vite targets the browser and does not inject a Node process global. Code (often a dependency) reads process.env.NODE_ENV directly, so the built bundle throws process is not defined when it runs.

What this error means

The build succeeds but the app or a headless test in CI throws "Uncaught ReferenceError: process is not defined", or a prerender step crashes on the same reference.

vite
Uncaught ReferenceError: process is not defined
    at node_modules/some-lib/index.js:1:1

Common causes

Code reads process.env directly in browser code

Vite exposes env via import.meta.env, not process.env. A direct process.env reference has no value in the browser bundle.

A dependency assumes a Node environment

A library written for Node references process at module load, and Vite does not shim it for the browser.

How to fix it

Use import.meta.env for your own code

Read Vite environment variables through import.meta.env instead of process.env.

src/config.ts
// before: process.env.NODE_ENV
const mode = import.meta.env.MODE

Define the global for a dependency that needs it

When a dependency hard-requires process.env, define the specific values at build time.

vite.config.js
export default {
  define: { 'process.env.NODE_ENV': JSON.stringify('production') }
}

How to prevent it

  • Use import.meta.env for env in browser code.
  • Audit dependencies that assume Node globals before bundling for the browser.
  • Avoid blanket define of all of process.env; define only what is needed.

Frequently asked questions

What causes ""process is not defined""?
Vite exposes env via import.meta.env, not process.env. A direct process.env reference has no value in the browser bundle.
How do I fix "process is not defined"?
Read Vite environment variables through import.meta.env instead of process.env.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card