安装cross-env
npm i cross-env -D
配置package.json命令
"scripts": { "serve": "cross-env REACT_APP_ENV=development node scripts/start.js", "build": "cross-env REACT_APP_ENV=production node scripts/build.js", "uat": "cross-env REACT_APP_ENV=uat node scripts/build.js", "sit": "cross-env REACT_APP_ENV=sit node scripts/build.js" },
创建.env ,.env.development ,.env.production ,.env.uat ,.env.sit,
- .env
REACT_APP_ENV=development
- .env.development
REACT_APP_ENV=development
- .env.production
REACT_APP_ENV=production
- .env.uat
REACT_APP_ENV=uat
- .env.sit
REACT_APP_ENV=sit
项目中获取当前环境变量
console.log(process.env.REACT_APP_ENV)