【发布时间】:2017-09-05 13:33:40
【问题描述】:
我需要为每个开发/生产服务器加载不同的 .env 文件。
Dev/Prod Server | .env file
---------------------------------------------
localhost | .env.development.local
development | .env.development
qa | .env.qa
production | .env.production
所以,我尝试以这种方式编辑 package.json
"start-js": "react-scripts start .env.development.local",
"start": " npm-run-all -p watch-css start-js",
"build:dev": "npm run build-css && react-scripts build .env.development",
"build:qa": "npm run build-css && react-scripts build .env.qa",
"build:pro": "npm run build-css && react-scripts build .env.production"
但如果我执行build:* 命令,加载的文件始终是 .env.production,因为 NODE_ENV 是生产环境。是否可以使用"build:* 命令加载其他 .env 文件?
【问题讨论】:
-
尝试查看 dotenv。您可以使用它加载不同的环境文件
-
@ReiDien 带有 dotenv 的
build:qa的脚本应该是什么?npm run build-css && react-scripts dotenv_config_path=qa.env不认识dotenv_config_path
标签: reactjs environment-variables create-react-app