Use cross-env for variable Settings in different environments

  1. Installation of cross – env
npm i --save-dev cross-env
Copy the code
  1. Write it in package.json and set a variable, which I’ll call DEPLOY
"scripts": {
    "build:dev": "cross-env DEPLOY=dev node build/build.js"."build:server1": "cross-env DEPLOY=server1 node build/build.js"."build:server2": "cross-env DEPLOY=server2 node build/build.js".Copy the code

3. How do I obtain the value of DEPLOY?process.env.DEPLOY

const hostArr = {
  server1: 'http://xx.xxx.com:8081'.server2: 'http://xx.xxx.com:8082'.dev: 'http://127.0.0.1:8080'
}
const host = hostArr[process.env.DEPLOY]; // Use a different address depending on DEPLOY
Copy the code
  1. Run the commandnpm run build:devornpm run buld:server1You can set different DEPLOY values