REACT_APP_* custom variables in .env* are undefined in development
See original GitHub issueIs 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
node -v
: 8.6.0npm -v
: 5.5.1yarn --version
(if you use Yarn): 1.3.2npm ls react-scripts
(if you haven’t ejected): 1.0.17
Then, specify:
- Operating system: macOS 10.11
- 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
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (3 by maintainers)
Top Results From Across the Web
react evironment variables .env return undefined
If the development server was running, stop it then rerun using npm start it. I really struggled with this (variable is an undefined...
Read more >Can't Read Environment Variable In React?
env file requires an application to be reloaded. Let's do this and try again: Undefined Once Again. Still no success, let's find out...
Read more >React app REACT_APP_ env variables always undefined
Hello everyone, I have a simple setup, trying to add IS_STAGING env variable to my app and I did as following I added...
Read more >Using environment variables in React | by Sam Pakvis - Medium
When working with React, environment variables are variables that are available through a global process.env Object. That global Object is provided by your ......
Read more >Understanding React Native env variables - LogRocket Blog
Variables can play different roles in React Native. For example, you can use CSS variables in React Native to store and implement custom...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.