Skip to content

Commit d173f9e

Browse files
authored
docs: distinguish the two "env var"s using different words (#18941)
1 parent bcdb51a commit d173f9e

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

docs/config/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default {
6161

6262
## Conditional Config
6363

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:
6565

6666
```js twoslash
6767
import { defineConfig } from 'vite'

docs/guide/env-and-mode.md

+39-36
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Env Variables and Modes
22

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
46

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:
68

79
- **`import.meta.env.MODE`**: {string} the [mode](#modes) the app is running in.
810

@@ -14,7 +16,31 @@ Vite exposes env variables on the special **`import.meta.env`** object, which ar
1416

1517
- **`import.meta.env.SSR`**: {boolean} whether the app is running in the [server](./ssr.md#conditional-logic).
1618

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.
31+
32+
```js
33+
console.log(import.meta.env.VITE_SOME_KEY) // "123"
34+
console.log(import.meta.env.DB_PASSWORD) // undefined
35+
```
36+
37+
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
1844
1945
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):
2046
@@ -34,27 +60,7 @@ Vite will always load `.env` and `.env.local` in addition to the mode-specific `
3460
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`.
3561
3662
`.env` files are loaded at the start of Vite. Restart the server after making changes.
37-
:::
3863
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.
49-
50-
```js
51-
console.log(import.meta.env.VITE_SOME_KEY) // "123"
52-
console.log(import.meta.env.DB_PASSWORD) // undefined
53-
```
54-
55-
:::tip Env parsing
56-
57-
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.
5864
:::
5965
6066
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
6874
NEW_KEY3=test$KEY # test123
6975
```
7076
71-
If you want to customize the env variables prefix, see the [envPrefix](/config/shared-options.html#envprefix) option.
72-
7377
:::warning SECURITY NOTES
7478
7579
- `.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.
7680
7781
- 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+
:::
7984
8085
::: details Expanding variables in reverse order
8186
@@ -94,7 +99,7 @@ To avoid interop issues, it is recommended to avoid relying on this behavior. Vi
9499
95100
:::
96101
97-
### IntelliSense for TypeScript
102+
## IntelliSense for TypeScript
98103
99104
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_`.
100105
@@ -124,11 +129,12 @@ If your code relies on types from browser environments such as [DOM](https://git
124129
:::warning Imports will break type augmentation
125130
126131
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+
127133
:::
128134
129-
## HTML Env Replacement
135+
## HTML Constant Replacement
130136
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:
132138
133139
```html
134140
<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
145151
146152
This means when running `vite build`, it will load the env variables from `.env.production` if there is one:
147153
148-
```
149-
# .env.production
154+
```[.env.production]
150155
VITE_APP_TITLE=My App
151156
```
152157
@@ -160,19 +165,17 @@ vite build --mode staging
160165
161166
And create a `.env.staging` file:
162167
163-
```
164-
# .env.staging
168+
```[.env.staging]
165169
VITE_APP_TITLE=My App (staging)
166170
```
167171
168172
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:
169173
170-
```
171-
# .env.testing
174+
```[.env.testing]
172175
NODE_ENV=development
173176
```
174177
175-
## NODE_ENV and Modes
178+
### NODE_ENV and Modes
176179
177180
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:
178181

docs/guide/features.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Alternatively, you can add `vite/client` to `compilerOptions.types` inside `tsco
137137
This will provide the following type shims:
138138

139139
- Asset imports (e.g. importing an `.svg` file)
140-
- Types for the Vite-injected [env variables](./env-and-mode#env-variables) on `import.meta.env`
140+
- Types for the Vite-injected [constant variables](./env-and-mode#env-variables) on `import.meta.env`
141141
- Types for the [HMR API](./api-hmr) on `import.meta.hot`
142142

143143
::: tip

0 commit comments

Comments
 (0)