Skip to content

Env Variables and Modes

Vite exposes certain constants under the special import.meta.env object. These constants are defined as global variables during dev and statically replaced at build time to make tree-shaking effective.

Example

Built-in Constants

Some built-in constants are available in all cases:

  • import.meta.env.MODE : {string} the mode the app is running in.

  • import.meta.env.BASE_URL : {string} the base url the app is being served from. This is determined by the base config option .

  • import.meta.env.PROD : {boolean} whether the app is running in production (running the dev server with NODE_ENV='production' or running an app built with NODE_ENV='production' ).

  • import.meta.env.DEV : {boolean} whether the app is running in development (always the opposite of import.meta.env.PROD )

  • import.meta.env.SSR : {boolean} whether the app is running in the server .

Env Variables

Vite exposes env variables under import.meta.env object as strings automatically.

To prevent accidentally leaking env variables to the client, only variables prefixed with VITE_ are exposed to your Vite-processed code. e.g. for the following env variables:

.env

Only VITE_SOME_KEY will be exposed as import.meta.env.VITE_SOME_KEY to your client source code, but DB_PASSWORD will not.

If you want to customize the env variables prefix, see the envPrefix option.

Env parsing

As shown above, VITE_SOME_KEY is a number but returns a string when parsed. The same would also happen for boolean env variables. Make sure to convert to the desired type when using it in your code.

.env Files

Vite uses dotenv to load additional environment variables from the following files in your environment directory :

Env Loading Priorities

An env file for a specific mode (e.g. .env.production ) will take higher priority than a generic one (e.g. .env ).

Vite will always load .env and .env.local in addition to the mode-specific .env.[mode] file. Variables declared in mode-specific files will take precedence over those in generic files, but variables defined only in .env or .env.local will still be available in the environment.

In addition, environment variables that already exist when Vite is executed have the highest priority and will not be overwritten by .env files. For example, when running VITE_SOME_KEY=123 vite build .

.env files are loaded at the start of Vite. Restart the server after making changes.

Bun users

When using Bun , be aware that Bun automatically loads .env files before your script runs. This built-in behavior loads environment variables directly into process.env and can interfere with Vite's feature, as it respects existing process.env values. See oven-sh/bun#5515 for workarounds.

Also, Vite uses dotenv-expand to expand variables written in env files out of the box. To learn more about the syntax, check out their docs .

Note that if you want to use $ inside your environment value, you have to escape it with \ .

.env

SECURITY NOTES

  • .env.*.local files are local-only and can contain sensitive variables. You should add *.local to your .gitignore to avoid them being checked into git.

  • Since any variables exposed to your Vite source code will end up in your client bundle, VITE_* variables should not contain any sensitive information.

Expanding variables in reverse order

Vite supports expanding variables in reverse order. For example, the .env below will be evaluated as VITE_FOO=foobar , VITE_BAR=bar .

.env

This does not work in shell scripts and other tools like docker compose . That said, Vite supports this behavior as this has been supported by dotenv-expand for a long time and other tools in JavaScript ecosystem uses older versions that supports this behavior.

To avoid interop issues, it is recommended to avoid relying on this behavior. Vite may start emitting warnings for this behavior in the future.

IntelliSense for TypeScript

By default, Vite provides type definitions for import.meta.env in vite/client.d.ts . While you can define more custom env variables in .env.[mode] files, you may want to get TypeScript IntelliSense for user-defined env variables that are prefixed with VITE_ .

To achieve this, you can create an vite-env.d.ts in src directory, then augment ImportMetaEnv like this:

vite-env.d.ts

If your code relies on types from browser environments such as DOM and WebWorker , you can update the lib field in tsconfig.json .

tsconfig.json