Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change React js environment property file (.env) at runtime

Tags:

reactjs

Currently I am developing a react application. I have created a .env file and am using the environment variables in all other pages but am not able to see the .env file in the build folder. Is there any way to change the environment variables at run time in the build file?

This will work when changing the hostname and port, etc. manually and then cresting the build. Is there any way to change the variables after creation of the build?

Any help will appropriated.

Thanks, Riya

like image 587
Riya Kumari Avatar asked Nov 07 '22 22:11

Riya Kumari


1 Answers

The answer to your question can be found here:
Create React App Docs: Adding Custom Environment Variables

The environment variables are embedded during the build time. Since Create React App produces a static HTML/CSS/JS bundle, it can’t possibly read them at runtime. To read them at runtime, you would need to load HTML into memory on the server and replace placeholders in runtime, just like described here. Alternatively you can rebuild the app on the server anytime you change them.

like image 59
yagiro Avatar answered Nov 14 '22 21:11

yagiro