When a Vite‑powered app works perfectly in development but crashes in production with errors like import.meta.env.VITE_SUPABASE_URL is undefined, the root cause is usually a mis‑handled environment variable.
Vite environment variables are defined as values prefixed with VITE_ that are inlined at build time, making them available to browser code.
Why Vite Variables Appear Undefined in Production
Vite replaces every import.meta.env.VITE_* reference with a literal value during the vite build step. If a variable is added after the build, the compiled bundle never sees it, resulting in undefined at runtime. This behavior is documented by Vite: "Vite replaces import.meta.env values when the app is built, and only variables that start with VITE_ reach browser code" (Vite docs).
Additional reasons include:
- Missing VITE_ prefix – variables without the prefix are stripped out.
- Reading
process.envin the browser – Vite only inlinesimport.meta.envvalues. - Using a stale development server that never re‑reads the updated
.envfile.
Correct Way to Define and Use Vite Environment Variables
Follow these steps to ensure your variables are correctly embedded:
- Create a dedicated file such as
.env.production(or.envfor all modes). - Prefix every variable you need in the browser with
VITE_, e.g.,VITE_API_BASE=https://api.example.com. - Commit the file to your repo only if it contains non‑secret values; keep secrets on the CI/CD platform.
- Configure your CI pipeline to set these variables on the host before invoking
npm run buildorvite build. - Reference them in code via
import.meta.env.VITE_API_BASE. The value is statically replaced, enabling tree‑shaking.
Vite reads .env files once at startup and inlines each VITE_ value as literal text at build time (confirmed in Vite 8.1, June 2026) (Env Variables and Modes | Vite).
Common Pitfalls and How to Debug Them
When you encounter undefined variables, run through this checklist:
- Check the prefix: Ensure the variable name starts with
VITE_. - Re‑build after changes: Any change to
.envrequires a freshvite build. - Avoid dynamic lookups: Accessing
import.meta.env[variableName]can be stripped by the compiler. - Inspect the bundle: Open the built JavaScript in DevTools and verify that the value is a literal string, not an empty object.
- Do not use process.env: In the browser,
process.envis undefined unless explicitly polyfilled.
If the issue persists, run the Vite env validator (available in the Vite CLI) to catch syntax errors early.
Deploying Securely – Keeping Secrets Out of the Browser
Only non‑sensitive data should ever be exposed via VITE_ variables. Secrets such as API keys, database passwords, or private tokens must remain on the server side. Use server‑side endpoints or serverless functions to interact with protected resources, and keep the client‑side code free of privileged credentials.
By separating concerns, you avoid accidental leakage and comply with best security practices.
Frequently Asked Questions
Why does import.meta.env.VITE_* work in dev but not after deployment?
During development Vite reads the .env file on each request, but in production the values are inlined at build time. If the variable was added after the build, the compiled code still contains the old (or missing) value.
Can I use process.env in a Vite project?
No. Vite only replaces import.meta.env variables that start with VITE_. Using process.env in browser code will result in undefined unless you add a custom plugin.
Do I need to rebuild the app for every environment change?
Yes. Since Vite inlines the values, any change to environment variables requires a fresh build to propagate the new literals.
How can I verify that my variables are correctly inlined?
Open the production bundle in the browser’s DevTools and search for the literal value you expect. If you see the string directly, the variable was correctly inlined.
What is the safest way to store API keys needed by the frontend?
Never store secret keys in VITE_ variables. Instead, create a backend endpoint that proxies the request, keeping the secret on the server.
At Neptune Infotech, we help businesses implement robust Vite configurations and secure deployment pipelines, ensuring your web applications run flawlessly in production.