You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/config/index.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ export default {
61
61
62
62
## Conditional Config
63
63
64
-
If the config needs to conditionally determine options based on the command (`serve` or `build`), the [mode](/guide/env-and-mode) being used, if it's an SSR build (`isSsrBuild`), or is previewing the build (`isPreview`), it can export a function instead:
64
+
If the config needs to conditionally determine options based on the command (`serve` or `build`), the [mode](/guide/env-and-mode#mode) being used, if it's an SSR build (`isSsrBuild`), or is previewing the build (`isPreview`), it can export a function instead:
Copy file name to clipboardExpand all lines: docs/guide/env-and-mode.md
+39-36
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,10 @@
1
1
# Env Variables and Modes
2
2
3
-
## Env Variables
3
+
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.
4
+
5
+
## Built-in constants
4
6
5
-
Vite exposes env variables on the special **`import.meta.env`** object, which are statically replaced at build time. Some built-in variables are available in all cases:
7
+
Some built-in constants are available in all cases:
6
8
7
9
-**`import.meta.env.MODE`**: {string} the [mode](#modes) the app is running in.
8
10
@@ -14,7 +16,31 @@ Vite exposes env variables on the special **`import.meta.env`** object, which ar
14
16
15
17
-**`import.meta.env.SSR`**: {boolean} whether the app is running in the [server](./ssr.md#conditional-logic).
16
18
17
-
## `.env` Files
19
+
## Env Variables
20
+
21
+
Vite exposes env variables under `import.meta.env` object as strings automatically.
22
+
23
+
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:
24
+
25
+
```[.env]
26
+
VITE_SOME_KEY=123
27
+
DB_PASSWORD=foobar
28
+
```
29
+
30
+
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](/config/shared-options.html#envprefix) option.
38
+
39
+
:::tip Env parsing
40
+
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.
41
+
:::
42
+
43
+
### `.env` Files
18
44
19
45
Vite uses [dotenv](https://github.com/motdotla/dotenv) to load additional environment variables from the following files in your [environment directory](/config/shared-options.md#envdir):
20
46
@@ -34,27 +60,7 @@ Vite will always load `.env` and `.env.local` in addition to the mode-specific `
34
60
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`.
35
61
36
62
`.env` files are loaded at the start of Vite. Restart the server after making changes.
37
-
:::
38
63
39
-
Loaded env variables are also exposed to your client source code via `import.meta.env` as strings.
40
-
41
-
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:
42
-
43
-
```[.env]
44
-
VITE_SOME_KEY=123
45
-
DB_PASSWORD=foobar
46
-
```
47
-
48
-
Only `VITE_SOME_KEY` will be exposed as `import.meta.env.VITE_SOME_KEY` to your client source code, but `DB_PASSWORD` will not.
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.
58
64
:::
59
65
60
66
Also, Vite uses [dotenv-expand](https://github.com/motdotla/dotenv-expand) to expand variables written in env files out of the box. To learn more about the syntax, check out [their docs](https://github.com/motdotla/dotenv-expand#what-rules-does-the-expansion-engine-follow).
@@ -68,14 +74,13 @@ NEW_KEY2=test\$foo # test$foo
68
74
NEW_KEY3=test$KEY # test123
69
75
```
70
76
71
-
If you want to customize the env variables prefix, see the [envPrefix](/config/shared-options.html#envprefix) option.
72
-
73
77
:::warning SECURITY NOTES
74
78
75
79
- `.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.
76
80
77
81
- Since any variables exposed to your Vite source code will end up in your client bundle, `VITE_*` variables should _not_ contain any sensitive information.
78
-
:::
82
+
83
+
:::
79
84
80
85
::: details Expanding variables in reverse order
81
86
@@ -94,7 +99,7 @@ To avoid interop issues, it is recommended to avoid relying on this behavior. Vi
94
99
95
100
:::
96
101
97
-
### IntelliSense for TypeScript
102
+
## IntelliSense for TypeScript
98
103
99
104
By default, Vite provides type definitions for `import.meta.env` in [`vite/client.d.ts`](https://github.com/vitejs/vite/blob/main/packages/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_`.
100
105
@@ -124,11 +129,12 @@ If your code relies on types from browser environments such as [DOM](https://git
124
129
:::warning Imports will break type augmentation
125
130
126
131
If the `ImportMetaEnv` augmentation does not work, make sure you do not have any `import` statements in `vite-env.d.ts`. See the [TypeScript documentation](https://www.typescriptlang.org/docs/handbook/2/modules.html#how-javascript-modules-are-defined) for more information.
132
+
127
133
:::
128
134
129
-
## HTML Env Replacement
135
+
## HTML Constant Replacement
130
136
131
-
Vite also supports replacing env variables in HTML files. Any properties in `import.meta.env` can be used in HTML files with a special `%ENV_NAME%` syntax:
137
+
Vite also supports replacing constants in HTML files. Any properties in `import.meta.env` can be used in HTML files with a special `%CONST_NAME%` syntax:
132
138
133
139
```html
134
140
<h1>Vite is running in%MODE%</h1>
@@ -145,8 +151,7 @@ By default, the dev server (`dev` command) runs in `development` mode and the `b
145
151
146
152
This means when running `vitebuild`, it will load the env variables from `.env.production` if there is one:
147
153
148
-
```
149
-
# .env.production
154
+
```[.env.production]
150
155
VITE_APP_TITLE=My App
151
156
```
152
157
@@ -160,19 +165,17 @@ vite build --mode staging
160
165
161
166
And create a `.env.staging` file:
162
167
163
-
```
164
-
# .env.staging
168
+
```[.env.staging]
165
169
VITE_APP_TITLE=My App (staging)
166
170
```
167
171
168
172
As `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:
169
173
170
-
```
171
-
# .env.testing
174
+
```[.env.testing]
172
175
NODE_ENV=development
173
176
```
174
177
175
-
## NODE_ENV and Modes
178
+
### NODE_ENV and Modes
176
179
177
180
It's important to note that `NODE_ENV` (`process.env.NODE_ENV`) and modes are two different concepts. Here's how different commands affect the `NODE_ENV` and mode:
0 commit comments