So I'm deploying a MERN stack app to heroku, and I have two .env files. One is for the nodejs backend, and one is for the react frontend. In heroku, you can set environment variables but they act essentially as a .env file in the root directory. However, my client folder is nested inside the node.js
-
2Hi, please provide some code and error logsSmileDeveloper– SmileDeveloper2020-07-09 21:54:15 +00:00Commented Jul 9, 2020 at 21:54
-
Hi Mahesh Natamai, how did you solve this issue? I'm having exactly the same issue.daniel blythe– daniel blythe2020-11-24 15:50:19 +00:00Commented Nov 24, 2020 at 15:50
Add a comment
|
2 Answers
If you're using react-scripts, you just need to preface any React env variables with REACT_APP_. This is how it knows where they belong when it is building your app. So just add them to your code and to the config vars on Heroku as REACT_APP_NAME_OF_YOUR_ENV_VAR and you should be good.
Here's the docs from Create React App on how to do it using react-scripts and adding the env variables and here it is for Heroku
2 Comments
Mahesh Natamai
Thanks for the help, didn't know that heroku config variables get passed down to the react app.
daniel blythe
Hi, how are you getting the Heroku env variables to be passed down? I can't get access to any in React which is in the client folder. The variables in Heroku are already prefixed with REACT_APP_ and have dotenv package in both root and client folder. Any help appreicated. Thanks