Skip to content

Commit 85c31cf

Browse files
Jost Schultehaoqunjiang
Jost Schulte
authored andcommitted
docs: extend mode and env vars guide (#2657) [ci skip]
1 parent 45dcbc5 commit 85c31cf

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

docs/guide/mode-and-env.md

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
1-
# Environment Variables and Modes
1+
# Modes and Environment Variables
2+
3+
## Modes
4+
5+
**Mode** is an important concept in Vue CLI projects. By default, there are three modes:
6+
7+
- `development` is used by `vue-cli-service serve`
8+
- `test` is used by `vue-cli-service test:unit`
9+
- `production` is used by `vue-cli-service build` and `vue-cli-service test:e2e`
10+
11+
You can overwrite the default mode used for a command by passing the `--mode` option flag. For example, if you want to use development variables in the build command:
12+
13+
```
14+
vue-cli-service build --mode development
15+
```
16+
17+
When running `vue-cli-service`, environment variables are loaded from all [corresponding files](#environment-variables). If they don't contain a `NODE_ENV` variable, it will be set accordingly. For example, `NODE_ENV` will be set to `"production"` in production mode, `"test"` in test mode, and defaults to `"development"` otherwise.
18+
19+
Then `NODE_ENV` will determine the primary mode your app is runnning in - development, production or test - and consequently, what kind of webpack config will be created.
20+
21+
With `NODE_ENV` set to "test" for example, Vue CLI creates a webpack config that is intended to be used and optimized for unit tests. It doesn't process images and other assets that are unnecessary for unit tests.
22+
23+
Similarly, `NODE_ENV=development` creates a webpack configuration which enables HMR, doesn't hash assets or create vendor bundles in order to allow for fast re-builds when running a dev server.
24+
25+
When you are running `vue-cli-service build`, your `NODE_ENV` should always be set to "production" to obtain an app ready for deployment, regardless of the environment you're deploying to.
26+
27+
::: warning NODE_ENV
28+
`NODE_ENV` and Vue CLI's mode are tightly linked. If you have set `NODE_ENV` in your environment outside of `.env` files, it will always determine the mode. You should either remove it or explicitly set `NODE_ENV` when running `vue-cli-service` commands, e.g. `NODE_ENV=production vue-cli-service build`.
29+
:::
30+
31+
## Environment Variables
232

333
You can specify env variables by placing the following files in your project root:
434

@@ -22,32 +52,10 @@ Loaded variables will become available to all `vue-cli-service` commands, plugin
2252

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

25-
In addition, environment variables that already exist when Vue CLI is bootstrapped have the highest priority and will not be overwritten by `.env` files.
26-
:::
27-
28-
::: warning NODE_ENV
29-
If you have a default `NODE_ENV` in your environment, you should either remove it or explicitly set `NODE_ENV` when running `vue-cli-service` commands.
55+
In addition, environment variables that already exist when Vue CLI is executed have the highest priority and will not be overwritten by `.env` files.
3056
:::
3157

32-
## Modes
33-
34-
**Mode** is an important concept in Vue CLI projects. By default, there are three modes in a Vue CLI project:
35-
36-
- `development` is used by `vue-cli-service serve`
37-
- `production` is used by `vue-cli-service build` and `vue-cli-service test:e2e`
38-
- `test` is used by `vue-cli-service test:unit`
39-
40-
Note that a mode is different from `NODE_ENV`, as a mode can contain multiple environment variables. That said, each mode does set `NODE_ENV` to the same value by default - for example, `NODE_ENV` will be set to `"development"` in development mode.
41-
42-
You can set environment variables only available to a certain mode by postfixing the `.env` file. For example, if you create a file named `.env.development` in your project root, then the variables declared in that file will only be loaded in development mode.
43-
44-
You can overwrite the default mode used for a command by passing the `--mode` option flag. For example, if you want to use development variables in the build command, add this to your `package.json` scripts:
45-
46-
```
47-
"dev-build": "vue-cli-service build --mode development",
48-
```
49-
50-
## Example: Staging Mode
58+
### Example: Staging Mode
5159

5260
Assuming we have an app with the following `.env` file:
5361

@@ -68,7 +76,7 @@ VUE_APP_TITLE=My App (staging)
6876

6977
In both cases, the app is built as a production app because of the `NODE_ENV`, but in the staging version, `process.env.VUE_APP_TITLE` is overwritten with a different value.
7078

71-
## Using Env Variables in Client-side Code
79+
### Using Env Variables in Client-side Code
7280

7381
Only variables that start with `VUE_APP_` will be statically embedded into the client bundle with `webpack.DefinePlugin`. You can access them in your application code:
7482

@@ -97,7 +105,7 @@ module.exports = {
97105
```
98106
:::
99107

100-
## Local Only Variables
108+
### Local Only Variables
101109

102110
Sometimes you might have env variables that should not be committed into the codebase, especially if your project is hosted in a public repository. In that case you should use an `.env.local` file instead. Local env files are ignored in `.gitignore` by default.
103111

0 commit comments

Comments
 (0)