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/guide/mode-and-env.md
+35-27Lines changed: 35 additions & 27 deletions
Original file line number
Diff line number
Diff 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
2
32
3
33
You can specify env variables by placing the following files in your project root:
4
34
@@ -22,32 +52,10 @@ Loaded variables will become available to all `vue-cli-service` commands, plugin
22
52
23
53
An env file for a specific mode (e.g. `.env.production`) will take higher priority than a generic one (e.g. `.env`).
24
54
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.
30
56
:::
31
57
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:
Assuming we have an app with the following `.env` file:
53
61
@@ -68,7 +76,7 @@ VUE_APP_TITLE=My App (staging)
68
76
69
77
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.
70
78
71
-
## Using Env Variables in Client-side Code
79
+
###Using Env Variables in Client-side Code
72
80
73
81
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:
74
82
@@ -97,7 +105,7 @@ module.exports = {
97
105
```
98
106
:::
99
107
100
-
## Local Only Variables
108
+
###Local Only Variables
101
109
102
110
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.
0 commit comments