Full answer
A React app built with Vite runs in the user's browser. There is no "private" environment there. So the design of environment variables in Vite is about what gets copied into the bundle.
How it works. Vite reads .env files (.env, .env.local, .env.production and mode-specific files) and the process environment. Only variables prefixed with VITE_ are exposed to client code as import.meta.env.VITE_API_URL. Other variables, such as DB_PASSWORD, stay out of the bundle. Built-in values include import.meta.env.MODE, DEV, PROD, BASE_URL. During the build, Vite statically replaces these expressions with their values, which is why they are constants in the output and why changing them requires a new build.
Security rule. Everything in the client bundle can be read by anyone (View Source, network tab). A VITE_STRIPE_SECRET_KEY would be public the moment you deploy. Public keys (a Stripe publishable key, a Maps key restricted by domain, an analytics id) are fine. Secrets (database credentials, private API keys, signing keys) belong on a server or serverless function that your app calls. The VITE_ prefix is a guard against accidental exposure, but a developer can still name a secret with the prefix, so review variables in code review. Do not commit .env.local or files with secrets; commit an .env.example.
Deployment. Because values are baked in, build once per environment, or avoid per-environment builds by loading runtime configuration from a small /config.json or an injected script that the server generates. TypeScript users declare variable types in vite-env.d.ts.
Other tools differ: Next.js uses the NEXT_PUBLIC_ prefix for client variables and keeps others on the server, and Create React App used REACT_APP_.
The example runs a real Vite build with a secret and a public variable in a .env file, and searches the output.
import { build } from 'vite';
import { mkdtemp, readdir, readFile, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
const root = await mkdtemp(path.join(tmpdir(), 'vite-env-'));
await writeFile(path.join(root, '.env'), 'VITE_API_URL=https://api.example.com\nDB_PASSWORD=never-ship-this\n');
await writeFile(path.join(root, 'index.html'), '<div id="root"></div><script type="module" src="/main.js"></script>');
await writeFile(
path.join(root, 'main.js'),
'console.log(import.meta.env.VITE_API_URL, import.meta.env.DB_PASSWORD, import.meta.env.MODE);\n'
);
await build({ root, configFile: false, logLevel: 'silent', build: { outDir: path.join(root, 'out') } });
const assets = path.join(root, 'out', 'assets');
const files = await readdir(assets);
const bundle = await readFile(path.join(assets, files.find((f) => f.endsWith('.js'))), 'utf8');
console.log('public value is inlined: ' + bundle.includes('https://api.example.com'));
console.log('secret value is in the bundle: ' + bundle.includes('never-ship-this'));
console.log('MODE replaced with production: ' + bundle.includes('production'));
console.log('import.meta.env is left in the code: ' + bundle.includes('import.meta.env'));This ran a real Vite build of a tiny project in a temporary folder. The VITE_ variable was copied into the output, while the other variable was not. The script only verifies the inlining, and does not use React. Output of the run: public value is inlined: true secret value is in the bundle: false MODE replaced with production: true import.meta.env is left in the code: false
Key points
- Only variables prefixed with VITE_ reach client code through import.meta.env.
- Values are inlined at build time, so changes need a rebuild.
- Anything in the bundle is public. Never store secrets in client variables.
- Keep secrets on a server and expose only public configuration.
- Commit .env.example, not secret files. Next.js uses NEXT_PUBLIC_ in the same role.
Common mistakes
- Putting API secrets in VITE_ variables.
- Expecting to change a variable on the server without rebuilding the client.
- Committing .env files with real secrets.
- Assuming the VITE_ prefix makes a value private.
- Using one build for several environments without runtime configuration.
Follow-ups
- How would you provide different API URLs per environment without rebuilding?
- How do you type import.meta.env in TypeScript?
- How do server frameworks keep non-public variables off the client?
Tips
- State the rule: the bundle is public.
- Explain the prefix as a guard, not as security.
- Mention runtime configuration as the way to avoid per-environment builds.