Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty REACT_APP_ env vars in .env.production cannot override its counterpart in .env #4002

Closed
MrOrz opened this issue Feb 9, 2018 · 7 comments

Comments

@MrOrz
Copy link

MrOrz commented Feb 9, 2018

Is 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

  1. node -v: v8.9.0
  2. npm -v: 5.5.1
  3. npm ls react-scripts (if you haven’t ejected): react-scripts@1.1.0

Then, specify:

  1. Operating system: Mac OSX

Steps to Reproduce

  1. Create .env and write REACT_APP_ENDPOINT=http://localhost:8080
  2. Create .env.production and write REACT_APP_ENDPOINT=
  3. inApp.js put:
console.log('ENDPOINT', REACT_APP_ENDPOINT} + '/api');
  1. npm run build
  2. Manually open the built main..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

@iansu
Copy link
Contributor

iansu commented Feb 9, 2018

This does sound like a bug. I took a quick look at the dotenv source and it does allow you to set an empty environment variable. The empty value in .env.production should take precedence over the non-empty value in .env.

I think the workaround you came up with is a better way to handle this situation anyway but we should still look into what's going on here.

@heyimalex
Copy link
Contributor

heyimalex commented Feb 9, 2018

It looks like this was fixed in dotenv here. So, looks like we need to wait for 5.0 release.

@iansu
Copy link
Contributor

iansu commented Feb 9, 2018

Looks like 5.0 was released a couple of weeks ago so we just need to upgrade. Too bad we didn't notice that before the 1.1.1 release yesterday. 😞

@Timer
Copy link
Contributor

Timer commented Feb 9, 2018

Since this is potentially breaking for others who may rely on the behavior it's probably better to do this in 2.x only [dotenv called it a major].

@iansu
Copy link
Contributor

iansu commented Feb 9, 2018

Makes sense. I've submitted a PR to next only at this point. I won't make one of 1.x.

@gaearon
Copy link
Contributor

gaearon commented Feb 10, 2018

The fix will be available in the next 2.x alpha.
#4009 (comment)

@gaearon gaearon closed this as completed Feb 10, 2018
@bcarroll22
Copy link

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 the expand package causes issues that allow empty strings to be overridden.

@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants