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
Read More:
- [Solved] Vue project configurate test environment and package error
- Vue-cli2 sub environment packaging
- [Solved] Error: Node Sass does not yet support your current environment
- [Solved] Syntax Error: Error: Node Sass does not yet support your current environment:
- NPM run dev runs the Vue project and reports an error: Node Sass does not yet support your current environment
- [Solved] Syntax Error: Error: Node Sass does not yet support your current environment: Windows 64-bit with
- [Solved] Syntax Error: Error: Node Sass does not yet support your current environment: OS X Unsupported archi
- [Solved] VSCode package Error: Command failed: npm list –production –parseable –depth=99999 –loglevel=error
- How to Solve Vue cli configuration SCSS global variable error
- [Solved] ERROR Error: Cannot find module ‘vue-loader-v16/package.json‘
- The vue3 project Error: Cannot find module’vue-loader-v16/package.json’
- [Solved] electron Package and Startup Error: Error: ENOENT: no such file or directory, open ‘xxx/manifest.json‘‘
- [Solved] Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension
- [Solved] Error: Cannot find module ‘vue-template-compiler/package.json‘ Require stack:
- [Solved] VS Code Error: Vetur can‘t find ‘tsconfig.json‘ or ‘jsconfig.json‘
- [Solved] TypeError: Converting circular structure to JSON – JSON.stringify
- [Solved] JSON.parse() Error: Unexpected end of JSON input
- How to Solve JS error: Unexpected end of JSON input,Unexpected token u in JSON at position 0
- [Solved] Vue3 Error: Cant find variable: GlobalThis
- [Weex]Error in creating project NPM: unable to load file D:\program files\nodejs\node_ global\ weex.ps1 Because scripts are not allowed to run on this system.