The error, verbatim
Jump to the fix ↓Uncaught ReferenceError: process is not defined
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
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-valueand 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