With `vite build --mode staging`, which `.env` file's `VITE_API_ORIGIN` wins when several define it, and what overrides every file?
answer
- four files per mode
- specific beats generic
- local beats shared within a tier
- the shell outranks the files
- mode is not NODE_ENV
basics
~10 sVite loads .env, .env.local, .env.staging and .env.staging.local, and the mode files win: .env.staging.local, then .env.staging, then .env.local, then .env. A variable already set in the shell or CI environment beats every file.
solid answer
~40 sFor a mode, Vite reads four files from the env directory: `.env`, `.env.local`, `.env.[mode]` and `.env.[mode].local`. Mode-specific files outrank generic ones, and the `.local` file outranks its shared partner, so the order is `.env.staging.local`, `.env.staging`, `.env.local`, `.env`; a variable defined only in `.env` is still available. Above all of them sits the process environment: a variable that exists when Vite starts is never overwritten by a file. The mode comes from the command (`development` for `vite`, `production` for `vite build`), from `--mode` or from the `mode` option, and it is not `NODE_ENV`: `vite build --mode staging` still has `NODE_ENV` set to `production`.
code
ini · 12 lines# .env (every mode)
VITE_API_ORIGIN=https://api.charity.test
VITE_DONATIONS_ENABLED=false
# .env.local (every mode, git-ignored)
VITE_API_ORIGIN=http://localhost:4000
# .env.staging (mode staging only)
VITE_API_ORIGIN=https://staging-api.charity.test
# .env.production (mode production only)
VITE_DONATIONS_ENABLED=truego deeper
Recall the four file names and that a variable exported in the shell beats them all. Know that vite build defaults to mode production.
Explain the two ordering rules, mode-specific over generic and local over shared, and why --mode changes MODE but leaves NODE_ENV at production.
Use precedence on purpose: commit the mode files, keep personal overrides in .local files, and inject per-build values from CI, knowing they beat every file.
Decide how many modes an organisation's apps carry and what each may hold, so env files stay a reviewed contract instead of a tangle of local overrides.
## Which files load Vite resolves a **mode** first, then loads up to four files from the env directory, which is the project root unless `envDir` points elsewhere (`envDir: false` turns file loading off): - `.env` loads in every mode. - `.env.local` loads in every mode and is meant to stay out of git. - `.env.[mode]` loads only in that mode, for example `.env.staging`. - `.env.[mode].local` loads only in that mode and stays out of git. Files for other modes are ignored, so `vite build --mode staging` never reads `.env.production`. Add `*.local` to `.gitignore`, since `.local` files tend to hold values that are personal to one machine. ## Precedence, highest first 1. Variables that already exist in the process environment when Vite starts, for example `VITE_API_ORIGIN=https://ci.charity.test vite build`. 2. `.env.[mode].local` 3. `.env.[mode]` 4. `.env.local` 5. `.env` Two rules produce this order. A **mode-specific** file outranks a generic one, which surprises people who assume `.env.local` beats everything: it does not beat `.env.staging`. Within each tier, the **`.local`** file outranks its shared partner. Merging is per variable, so a name defined only in `.env` is still present in staging. The process environment wins because Vite never overwrites a variable that already exists. That is what lets a CI job inject a value for one build without editing any file. ## Mode versus `NODE_ENV` The mode chooses which files load and what `import.meta.env.MODE` reads. `NODE_ENV` decides whether the run is a production one, which is what `import.meta.env.PROD` and `DEV` report. | Command | `NODE_ENV` | Mode | |---|---|---| | `vite build` | `production` | `production` | | `vite build --mode staging` | `production` | `staging` | | `NODE_ENV=development vite build` | `development` | `production` | | `NODE_ENV=development vite build --mode development` | `development` | `development` | The dev server defaults to mode `development` and `vite build` to `production`. The `mode` config option overrides the default for both commands, and `--mode` overrides that. ## Traps worth knowing - `NODE_ENV=development` inside a mode's `.env` file is supported and makes that mode build in development style. `NODE_ENV=production` in a `.env` file is not supported: Vite warns and suggests setting it in the config instead. - A mode named `local` throws, because it would collide with the `.local` suffix. - The files are read when Vite starts. Values edited later reach a running dev server only after it restarts. - The prefix rule still applies after merging: an unprefixed variable from any file stays out of `import.meta.env`. - Precedence is decided by file name, never by which file was edited last. ## Setting modes up for a team - Commit `.env` and the `.env.[mode]` files: they describe each environment and belong in review. - Keep machine-specific values, such as a local API on another port, in `.env.local` or `.env.[mode].local`, which stay out of git. - Let CI inject per-build values through the environment rather than writing files during the job; they win over every file anyway. - Use the `mode` config option only when a project should default to a non-standard mode for both commands; otherwise `--mode` on the script is clearer. ## Finding out which value won 1. Log `import.meta.env.MODE` to confirm which mode actually ran. 2. Check that the file name matches that mode exactly, in the env directory. 3. List the variables exported in the shell or CI job; one of them may be overriding the files. 4. Restart the dev server after editing a file, then check again. ## A worked example With `.env` setting the production origin, `.env.local` setting `http://localhost:4000` and `.env.staging` setting the staging origin, `vite build --mode staging` uses the staging origin: the mode file outranks `.env.local`. Run the same command from a CI job that exports `VITE_API_ORIGIN`, and the exported value wins over all three files. Running the dev server instead, with plain `vite`, selects mode `development`: Vite reads `.env`, `.env.local` and `.env.development` if it exists, so the developer's `.env.local` origin, `http://localhost:4000`, wins unless a development file defines the same name. `VITE_DONATIONS_ENABLED` comes from `.env` in both cases, because no other loaded file sets it.
- Why does `vite build --mode development` still produce a production build?The mode only selects env files and sets `MODE`. `vite build` still sets `NODE_ENV` to `production`, so `import.meta.env.PROD` is true. For a development-style build, set `NODE_ENV=development` on the command or inside that mode's `.env` file.
- A CI job exports `VITE_API_ORIGIN` and `.env.staging.local` sets a different value; which one does the build use?The exported one. Variables that already exist in the process environment when Vite starts have the highest priority and are never overwritten by `.env` files, including `.local` ones.
- Why can't a project define a mode called `local`?Its file would be `.env.local`, which already means the git-ignored file for every mode. Vite throws an error rather than guess which meaning you intended.
Think of a stack of notes on a desk: notes written for this particular mode lie on top of the general ones, and a personal note sits above the shared note in each pair. You read the value from the highest note that mentions it, but an instruction someone hands you in person, the shell environment, overrides the whole stack.
- .env.local overrides every other env file, including .env.staging.
- A value in .env.staging replaces a variable already exported in the shell.
- Passing --mode staging also sets NODE_ENV to staging.
- In staging mode Vite loads only .env.staging and ignores .env.
- Putting NODE_ENV=production in a .env file is the supported way to force a production run.