Build-time configuration and public environment variables
Quick answer
Browser build-time configuration becomes part of the delivered application. In Vite, import.meta.env exposes built-in values and, by default, user variables prefixed VITE_.
Tooling examples
In the Vite template after setup, add this line to src/main.js and inspect the browser console. Requires Vite; not a plain Node snippet. Manual integration example — not executed here.
// vite-lab/src/main.js; requires Vite, not plain Node.console.log(import.meta.env.VITE_APP_LABEL);Expected observation: With VITE_APP_LABEL=Notebook, logs Notebook.
Run as an independent JavaScript snippet with Node. Local check available; consult validation evidence for what was asserted.
const raw = "false";console.log(Boolean(raw), raw === "true");Expected observation: true false
How the tooling works
Use public values such as an application label, and keep server credentials outside browser builds. Vite exposes custom env values as strings; parse and validate them explicitly. Mode selects environment files and need not be identical to NODE_ENV. Build-time replacement means changing a host environment variable after deployment does not generally change an already generated static bundle. A runtime configuration endpoint is an alternative for public values that need to change without rebuilding.
Common mistakes
Never put API private keys in VITE_ variables. A hidden UI does not hide the downloaded JavaScript.
Practice
Configure a public label in the template and verify its visible value in the built application. Confirm no credentials are present in the generated files.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Build-time configuration and public environment variables — Knowledge Check
Assess tooling contracts, diagnostic reasoning and transfer to a project.
By default, which custom env variable name is exposed to Vite client code?
Why should a browser build never contain a private API key?
What type are custom Vite env values exposed as?
In Build-time configuration and public environment variables, what is the expected observation for example 1 in its documented setup? // vite-lab/src/main.js; requires Vite, not plain Node. console.log(import.meta.env.VITE_APP_LABEL);
In Build-time configuration and public environment variables, what is the expected observation for example 2 in its documented setup? const raw = "false"; console.log(Boolean(raw), raw === "true");