Skip to content

Vite: VITE_ variables and build time

Vite gives real environment variables priority over .env files, exposes only VITE_ names, and inlines them at build. Wrap vite with npx getmyenv run.

Oct 10, 2026 · 3 min read

An encrypted padlock leads to one terminal command, a Vite build, and the built bundle.
getmyenv run passes the values to Vite. VITE_ names are written into the bundle at build.

1. The real environment wins

Variables that exist when Vite starts have the highest priority. A .env file does not replace them.

npx getmyenv run -- vite

run does not read .env files. Delete them when you are ready. Until you do, Vite still fills names the context does not set from them.

2. Only VITE_ names reach the browser code

run passes every name to the Vite process. Vite exposes only the ones that start with VITE_, or your envPrefix.

js
const apiUrl = import.meta.env.VITE_API_URL;

Vite loads .env files only after it reads the config. With run, every name is already in process.env, including in vite.config.

3. Values are inlined at build

vite build replaces import.meta.env.VITE_* with the values in the files it writes. Anyone who loads the site can read them. Build with the context you deploy. Production is Read-only, so the build machine needs a server token in GETMYENV_TOKEN:

npx getmyenv run production -- vite build

Keep API keys and other secrets out of VITE_ names. Call them from a server instead. set and import warn when a VITE_ name or value looks like a secret. A new value needs a new build.

4. Check what Vite gets

npx getmyenv run production --explain -- vite build

npx getmyenv run --explain lists every name and where it comes from. Never values.

Related docs: How run works, Contexts, Languages