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

REACT_APP_* custom variables in .env* are undefined in development #3568

Closed
suzyng83209 opened this issue Dec 9, 2017 · 12 comments
Closed

Comments

@suzyng83209
Copy link

suzyng83209 commented Dec 9, 2017

Is this a bug report?

yes

Can you also reproduce the problem with npm 4.x?

It's not an npm or yarn error

Which terms did you search for in User Guide?

custom environment variables

Environment

  1. node -v: 8.6.0
  2. npm -v: 5.5.1
  3. yarn --version (if you use Yarn): 1.3.2
  4. npm ls react-scripts (if you haven’t ejected): 1.0.17

Then, specify:

  1. Operating system: macOS 10.11
  2. Browser and version (if relevant): chrome

Steps to Reproduce

As the documentation specified, front end environment variables can be defined by exporting them to the terminal, defining them on the start script of with a .env file on the root.

My create-react-app is nested inside a backend express app with its own .env. However, I have a .env in the root of my front end app with REACT_APP_HOST=localhost.

In App.js, I console.log(process.env) and the only keys displayed are

{ NODE_ENV: 'development', PUBLIC_URL: '' }

It seems like its working for most other users and it may be that my app is nested but I'm unsure as to why environment variables defined in my .env are not showing up.

// .env
REACT_APP_HOST=localhost
// App.js
....
componentDidMount() {
    console.log(process.env);
}
// app structure
- fullApp
   - create-react-app
      - components
         - App.js
      - .env
   - index.js
   - .env

react-scripts@1.0.17

@Timer
Copy link
Contributor

Timer commented Dec 9, 2017

It doesn't look up your directory tree into fullApp/.env. The only file it reads is fullApp/create-react-app/.env.

Please make sure it's in the correct file.

@Timer
Copy link
Contributor

Timer commented Dec 9, 2017

If you exported REACT_APP_HOST in your shell, that overrides any .env setting as well.

@suzyng83209
Copy link
Author

I have not. In fact, process.env does not have any custom react vars when I log it out

@suzyng83209
Copy link
Author

Actually I have found out the reason why.

I did not realize that the environment variables are injected in build-time. (Don't know why I assumed they would be accessible in dev)

Thanks for your help though.

@ghost
Copy link

ghost commented Oct 3, 2018

So you can't use them in development ?!?

@Timer
Copy link
Contributor

Timer commented Oct 3, 2018

So you can't use them in development ?!?

Yes, you can. Are you experiencing behavior otherwise? If so, please file a new issue.

@jeroenhoebe
Copy link

Having the same problem on my hackintosh (OSX 10.11.6)
node: v10.3.0
npm: 6.4.1
react-scripts: 2.0.4

But the same project/code does work on my MacBook Pro (macOS 10.13.6)
node: v10.1.0
npm: 6.4.1
react-scripts: 2.0.4

@LuizFellype
Copy link

LuizFellype commented Nov 3, 2018

@jeroenhoebe I am having the same issue em MacBook here!

@vivmagarwal
Copy link

Re-starting the server may resolve the issue for most (If .env files were created while server was running)

control + c to stop the server
yarn start to start the server

@pmkgithub
Copy link

Having a similar issue with .env.development and .env.production custom env files - REACT_APP* env vars are undefined in my React code.

However, .env is working properly and my REACT_APP* env vars are properly exposed in my React code.

mac OS High Sierra v 10.13.6
node: v 8.11.2
npm: v 5.6.0
react-scripts: v 2.1.1

Additionally, I can verify the existence of these three files and their env var values via $cat .env.development and $cat .env.production and $ cat .env

This CRA app is a nested React app inside a node/express server directory. There are no .env, or .env* files in the root server directory. The .env, .env* files are in the root CRA app directory only.

Restarting server, closing IDE or clearing Browser cache does not remedy things.

@moparlakci
Copy link

You should not put your env files in the src folder, put it in the folder above.. ;)

@Luckygirlllll
Copy link

the same problem, I have restarted server, but variable is still undefined

@lock lock bot locked and limited conversation to collaborators Jan 14, 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

8 participants