2017-09-05 156 views
1

我需要爲每個開發/生產服務器加載一個不同的.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文件可能嗎?

+0

嘗試看看dotenv。你可以使用它加載不同的env文件 –

+0

@ReiDien使用dotenv編譯'build:qa'的腳本應該是什麼? 'npm run build-css && react-scripts dotenv_config_path = qa.env'不能識別'dotenv_config_path' – Webman

回答

1

我使用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"