Tag Archives: package.json Scripts

package.json Scripts configuration environment variable distinguishes development environment from production environment

Sometimes it is necessary to differentiate between packages from production and development environments to determine which base url or other variables you need to useMethod 1:

//windows System:
"builddev": "set NODE_ENV=dev&&vue-cli-service build",
"buildprod": "set NODE_ENV=prod&&vue-cli-service build",

//mac System:
"builddev": "export NODE_ENV=dev&&vue-cli-service build",
"buildprod": "export NODE_ENV=prod&&vue-cli-service build",

Method 2:
First install the dependencies: NPM install –save-dev cross-env
Then configure compatible Windows and MAC environments

"builddev": "cross-env NODE_ENV=dev vue-cli-service build",
"buildprod": "cross-env NODE_ENV=prod vue-cli-service build",

Finally, in the JS code, the corresponding value can be obtained through: process.env.node_env, which is used to judge the environment:
baseurl:process.env.NODE_ENV===’prod’?’http://prod.api.com’:’http://test.api.com’
console.log(process.env);// {BASE_URL: “”, NODE_ENV:” prod “}

Ps: The vUE – CLi is used here to configure NODE_ENV, other projects may be to configure WEBPACK_ENV, directly replace, then console. Log (process.env) to see the environment