Empty REACT_APP_ env vars in .env.production cannot override its counterpart in .env
See original GitHub issueIs this a bug report?
Yes.
When an REACT_APP_
environment variable is set in .env
, we cannot overwrite it with an empty string.
In our scenario, our app tries to connect to an development API server running on local machine, but in production the static files built with npm build
will be served directly with API server. The host name is set through an REACT_APP_ENDPOINT
environment variable, with it being localhost in .env
and being an empty string in .env.production
. However, the empty string somehow cannot override the settings in .env
.
In reality I worked-around this by putting REACT_APP_ENDPOINT
in .env.development
.
Also, for API servers we can also use proxies instead. However, I think it’s still a bit counter-intuitive that settings in .env.production
cannot override the one in .env
. That’s why I opened this issue.
Did you try recovering your dependencies?
It’s reproducible in fresh install of create-react-app
Which terms did you search for in User Guide?
env
then read through “Adding Custom Environment Variables”
Environment
node -v
: v8.9.0npm -v
: 5.5.1npm ls react-scripts
(if you haven’t ejected): react-scripts@1.1.0
Then, specify:
- Operating system: Mac OSX
Steps to Reproduce
- Create
.env
and writeREACT_APP_ENDPOINT=http://localhost:8080
- Create
.env.production
and writeREACT_APP_ENDPOINT=
- in
App.js
put:
console.log('ENDPOINT', REACT_APP_ENDPOINT} + '/api');
npm run build
- Manually open the built main.<hash>.js, search for 'ENDPOINT: ’
Expected Behavior
Find this in the built js:
console.log('ENDPOINT', '/api');
Actual Behavior
Find this in the built js:
console.log('ENDPOINT', 'http://localhost:8080/api')
Reproducible Demo
Repo
https://github.com/MrOrz/create-react-app-env-override/
Relevant file
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (6 by maintainers)
Top GitHub Comments
Hey everyone, just wanted to point out that I’m still running into this issue. I believe the bug here is actually being introduced by
dotenv-expand
. When I take it out, the variable overrides behave correctly, but something in theexpand
package causes issues that allow empty strings to be overridden.The fix will be available in the next 2.x alpha. https://github.com/facebook/create-react-app/pull/4009#issuecomment-364684770