REACT_APP_X env variable does not show up on front-end as process.env.REACT_APP_X
See original GitHub issueI have this start script in package.json
"start-dev": "REACT_APP_SECRET_CODE=development && react-scripts start",
on the front-end, I execute this
console.log(' => Process env => ',process.env);
It appears that process.env on the front-end is an empty object, but according to the docs I should expect this:
{
REACT_APP_SECRET_CODE:'development'
}
what am I doing wrong?
Issue Analytics
- State:
- Created 7 years ago
- Comments:15 (6 by maintainers)
Top Results From Across the Web
create react app not picking up .env files?
Had this same problem! The solution was to close the connection to my node server (you can do this with ...
Read more >Can't Read Environment Variable In React?
The second thing to remember is that, for security reasons, create-react-app does not allow you to define Environment Variables that do not start...
Read more >Using environment variables in React | by Sam Pakvis - Medium
Your front-end code will refer to the same environment variable ( process.env.API_URL ) on both environments (development/production), but because you defined ...
Read more >Adding Custom Environment Variables
You can read it from process.env.NODE_ENV . When you run npm start , it is always equal to 'development' , when you run...
Read more >React - Access Environment Variables from dotenv (.env)
React apps built with Create React App support dotenv environment variables out of the box, so all you need to do is add...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I created a new project and put
in it.
I am seeing an object being printed:
Next, let’s try a custom variable. The way you specify environment variables is incorrect.
The User Guide section I referred you to earlier also shows how to do it:
Note it’s
REACT_APP_SECRET_CODE=abcdef npm start
and notREACT_APP_SECRET_CODE=abcdef && npm start
. There is no&&
there.If I remove
&&
from your example then the logged object looks like this:As you can see the environment variable is now available. However if all you want is to tell if the app is running in production,
process.env.NODE_ENV
is enough. You don’t need to pass anything else.I hope this helps!
Np, I can figure it out, it may be that the project I am working on was modified so that the env variables no longer get sent to the front-end via the HTML. thanks for your help, at least I know absolutely what the expected behavior is. I will start from a new app and figure it out