使用.env和create-react-app加载不同的env变量
问题描述:
我需要为每个开发/生产服务器加载一个不同的.env文件。使用.env和create-react-app加载不同的env变量
Dev/Prod Server | .env file
---------------------------------------------
localhost | .env.development.local
development | .env.development
qa | .env.qa
production | .env.production
所以,我想用这种方式
"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"
编辑的package.json但如果我执行命令build:*
,加载的文件总是.env.production因为NODE_ENV是生产。用"build:*
命令加载其他.env文件可能吗?
答
我使用react-app-env和重命名.env文件解决了我的问题。
Dev/Prod Server | .env file
---------------------------------------------
qa | qa.env
而且build:qa
脚本,例如,成为:
"build": "npm run build-css && react-scripts build",
"build:qa": "react-app-env --env-file=qa.env build"
尝试看看dotenv。你可以使用它加载不同的env文件 –
@ReiDien使用dotenv编译'build:qa'的脚本应该是什么? 'npm run build-css && react-scripts dotenv_config_path = qa.env'不能识别'dotenv_config_path' – Webman