import.meta.env values are undefined after vite build
import.meta.env.VITE_API_URL is undefined in productionShort answer
Vite copies variables into your code at build time, and only those that start with VITE_. A missing prefix, an env file for the wrong mode, or a variable set on the server after the build all give undefined. Rename the variable, put it in the right file, and rebuild.
Fix it fast
1Find out which cause you have
Print what Vite sees for the production build. The keys that are listed are the only ones your code can read.
// main.js, temporary console.log(import.meta.env)2Match what you see, then jump to its fix
- 1
The key is absent from the logged objectThe variable name has no VITE_ prefixFix - 2
The key is present in dev, missing in the buildThe value lives in an env file for another modeFix - 3
Works when you build on your laptop, undefined from the pipeline or the Docker imageThe value is set on the server or in CI after the buildFix
- 1
What does the error look like?
import.meta.env.VITE_API_URL is undefined in productionThe value works with npm run dev but not after npm run build
Cause 1 of 3
The variable name has no VITE_ prefix
1Check
Only variables whose names start with VITE_ reach client code. Others, such as DB_PASSWORD, are kept out on purpose.
grep -n -v "^#" .env | cut -d= -f12Fix
Rename the variable to start with VITE_ and read it with that name. Do not put secrets in it, because it ends up in the public bundle.
# .env
VITE_API_URL=https://api.example.com3Verify
Rebuild and read the value.
npm run build && grep -r "api.example.com" dist | head -n 1Confirmed by vite.dev
Cause 2 of 3
The value lives in an env file for another mode
1Check
npm run dev uses development mode and vite build uses production mode. A file such as .env.development is not read by the build.
ls -a | grep "^.env"2Fix
Put the value in .env, or in .env.production, or build with the mode that matches the file.
# .env.production
VITE_API_URL=https://api.example.com
# or pick a mode explicitly (loads .env.staging)
npx vite build --mode staging3Verify
Rebuild and check the bundle.
npx vite buildConfirmed by vite.dev
Cause 3 of 3
The value is set on the server or in CI after the build
1Check
Values are statically replaced while vite build runs. Setting them later, at container start, changes nothing. The env file must exist in the build step, or the variable must be set in the command that runs it.
env | grep VITE_2Fix
Provide the variable to the build step itself.
VITE_API_URL=https://api.example.com npx vite build3Verify
Search the output for the value.
grep -r "api.example.com" dist | head -n 1Confirmed by vite.dev
How do I stop it coming back?
- Write the needed VITE_ names in a committed .env.example.
- Set build-time variables in the same step that runs vite build.
- Never put secrets in a VITE_ variable.
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:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix