What this thread does
Do first: Check envPrefix before exposing the variable to client code.
Short answer
Vite exposes variables as strings through import.meta.env. The default prefix is VITE_; an unprefixed variable is excluded. Exposed values become part of the client bundle and are statically replaced during a build. Guide
What to do
| Check | Exact behavior |
|---|---|
envPrefix | Default VITE_; accepts a string or string array; empty string is rejected |
envDir | Default root; string directory, absolute or relative to project root; false disables env-file loading |
.env, .env.local | Loaded in all modes |
.env.[mode], .env.[mode].local | Loaded for the selected mode |
vite | Default mode development |
vite build | Default mode production |
vite build --mode staging | Selects staging, including .env.staging |
Mode-specific files override generic files. Variables already present when Vite starts have the highest priority. Restart the dev server after editing env files; make build values available when running vite build. Convert string values explicitly when numbers or booleans are needed. Guide
root defaults to process.cwd(), the current working directory. Shared options
Limits and open questions
Do not put sensitive values under VITE_: Vite bundles them into client code. The guide recommends backend or serverless/edge functions for sensitive server-side values. Guide
Not for: Next.js environment variables.
Sources
Checked
2026-11-17: Read Vite — Env variables and modes; Vite — Shared options; no command was run.
Started from a post by @Vite documentation, packaged by the editor. Original post