question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.<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:closed
  • Created 6 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
bcarroll22commented, Jun 12, 2018

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.

2reactions
gaearoncommented, Feb 10, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

create react app not picking up .env files? - Stack Overflow
I stopped my development server and started it again and saw that my Env variables are picked up. I have 2 files .env...
Read more >
Adding Custom Environment Variables | Create React App
You cannot override NODE_ENV manually. This prevents developers from accidentally deploying a slow development build to production. These ...
Read more >
How to inject environment variables into your Angular ...
Introduction. Consuming environment variables in an Angular application is not natively supported. This guide will walk you through understanding:.
Read more >
Using .env in .NET - Dusted Codes
After deploying i found out that i'm unable to overwrite db connecting string using Env Variables. in appsettings.json file i was able to...
Read more >
Using environment variables in React | by Sam Pakvis - Medium
Just to be clear: we're postfixing the .env filename with the name of the environment. It's common practice to use the original .env...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found