Uncaught ReferenceError: process is not defined in a Vite app
Uncaught ReferenceError: process is not definedShort answer
Vite does not provide Node.js globals such as process to browser code. Code written for Create React App or webpack reads process.env and breaks. Use import.meta.env with a VITE_ prefix in your own code. For a library that needs a constant, supply it through the define option.
Fix it fast
1Find out which cause you have
Find every place that touches process in the code that ships to the browser.
grep -rn "process\.env" src/2Match what you see, then jump to its fix
What does the error look like?
Uncaught ReferenceError: process is not defined
Cause 1 of 2
Your own code reads process.env
1Check
The grep output lists lines such as process.env.REACT_APP_API_URL, copied from a Create React App project.
grep -rn "REACT_APP_\|process\.env" src/2Fix
Rename the variable with a VITE_ prefix in the env file and read it through import.meta.env. Only prefixed variables are exposed to client code.
// before
const url = process.env.REACT_APP_API_URL
// after
const url = import.meta.env.VITE_API_URL
# .env
VITE_API_URL=https://api.example.com3Verify
Restart the dev server so the env file is read again, then reload the page.
npm run devConfirmed by vite.dev
Cause 2 of 2
A dependency or an old snippet needs a replaced constant
1Check
Search the stack frame's file for the exact expression, for example process.env.SOME_FLAG.
grep -rn "process\.env\.SOME_FLAG" node_modules/some-package | head2Fix
Replace the exact expression at build time with the define option. Values must be JSON-serializable, so wrap strings in JSON.stringify.
// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
define: {
'process.env.SOME_FLAG': JSON.stringify('on'),
},
})3Verify
Restart the dev server and reload.
npm run devConfirmed by vite.dev
How do I stop it coming back?
- Use only import.meta.env in browser code, and keep process for vite.config and Node scripts.
- Search for process.env after moving a project from another bundler.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
import.meta.env values are undefined after vite build
The value works with npm run dev but not after npm run build
Vite copies variables into your code at build time, and only those that start with VITE_.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
npm start stops with Could not find a required file in a React app
Name: index.html
react-scripts checks for two files before the dev server starts: public/index.html and a src/index file.
3 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
npm audit stops with ENOLOCK because there is no lockfile
npm ERR! audit
npm audit sends the dependency tree from your lockfile to the registry.
2 causes, each with a fixBack end
npm says an override conflicts with a direct dependency
Override for react-error-overlay@^6.0.9 conflicts with direct dependency
npm refuses an override for a package you also depend on directly, unless both specs are exactly the same.
2 causes, each with a fix