import.meta.env object. These constants are defined as global variables during dev and statically replaced at build time to make tree-shaking effective.
Built-in Constants
Some built-in constants are available in all cases:The base url the app is being served from. This is determined by the
base config option.Whether the app is running in production (running the dev server with
NODE_ENV='production' or running an app built with NODE_ENV='production').Whether the app is running in development (always the opposite of
import.meta.env.PROD).Environment Variables
Vite exposes env variables under theimport.meta.env object as strings automatically.
Variables prefixed with VITE_ will be exposed in client-side source code after Vite bundling. To prevent accidentally leaking env variables to the client, avoid using this prefix. As an example, consider the following:
.env
VITE_SOME_KEY – "123" – will be exposed on the client, but the value of DB_PASSWORD will not. You can test this by adding the following to your code:
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:Variable Expansion
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
IntelliSense for TypeScript
By default, Vite provides type definitions forimport.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
tsconfig.json.
tsconfig.json
HTML Constant Replacement
Vite also supports replacing constants in HTML files. Any properties inimport.meta.env can be used in HTML files with a special %CONST_NAME% syntax:
import.meta.env, e.g. %NON_EXISTENT%, it will be ignored and not replaced, unlike import.meta.env.NON_EXISTENT in JS where it’s replaced as undefined.
Given that Vite is used by many frameworks, it is intentionally unopinionated about complex replacements like conditionals. Vite can be extended using an existing userland plugin or a custom plugin that implements the transformIndexHtml hook.
Modes
By default, the dev server (dev command) runs in development mode and the build command runs in production mode.
This means when running vite build, it will load the env variables from .env.production if there is one:
.env.production
import.meta.env.VITE_APP_TITLE.
In some cases, you may want to run vite build with a different mode to render a different title. You can overwrite the default mode used for a command by passing the --mode option flag. For example, if you want to build your app for a staging mode:
.env.staging file:
.env.staging
vite build runs a production build by default, you can also change this and run a development build by using a different mode and .env file configuration:
.env.testing
NODE_ENV and Modes
It’s important to note thatNODE_ENV (process.env.NODE_ENV) and modes are two different concepts. Here’s how different commands affect the NODE_ENV and mode:
| Command | NODE_ENV | Mode |
|---|---|---|
vite build | "production" | "production" |
vite build --mode development | "production" | "development" |
NODE_ENV=development vite build | "development" | "production" |
NODE_ENV=development vite build --mode development | "development" | "development" |
NODE_ENV and mode also reflect on its corresponding import.meta.env properties:
| Command | import.meta.env.PROD | import.meta.env.DEV |
|---|---|---|
NODE_ENV=production | true | false |
NODE_ENV=development | false | true |
NODE_ENV=other | false | true |
| Command | import.meta.env.MODE |
|---|---|
--mode production | "production" |
--mode development | "development" |
--mode staging | "staging" |