Adding a environment .env file to ReactJS project

⚠ Note: this feature is available with react-scripts@0.2.3 and higher.

WARNING: Do not store any secrets (such as private API keys) in your React app!

Environment variables are embedded into the build, meaning anyone can view them by inspecting your…


This content originally appeared on DEV Community and was authored by Rajesh Royal

⚠ Note: this feature is available with react-scripts@0.2.3 and higher.

WARNING: Do not store any secrets (such as private API keys) in your React app!

Environment variables are embedded into the build, meaning anyone can view them by inspecting your app's files.

So why not I just use a .js, ts, json file, well something is better than having nothing.

Sometimes we need to have multiple development environments [dev, stag, prod] and according to these environments you want to choose your variables, that's where .env files can be useful.

There are two ways you can use environment files in you react js project.

Method: 1.

If your project is built with CRA(create-react-app), then it becomes very easy.
Read the official documentation.

Adding Custom Environment Variables to CRA project

Long story short -

  • Create a .env file at the root of your project.
  • Variable name must start with REACT_APP_
  • Use it anywhere in react - process.env.REACT_APP_VAR_NAME

Quick sneak peak -

Adding Custom Environment Variables to CRA project

Method 2

For custom ReactJS setups, please refer to this stackoverflow. This link lands you directly to the working solution.

Adding an .env file to React Project

 
 

📝NOTE - Need to restart application after adding variable in .env file.

 
 
 
That's all for today! see you soon 👀. Oh! I forget to tell you that you can still make that heart red in top left corner or save this for later 🤘 Tada 🙋‍♂️


This content originally appeared on DEV Community and was authored by Rajesh Royal


Print Share Comment Cite Upload Translate Updates
APA

Rajesh Royal | Sciencx (2022-01-31T16:28:23+00:00) Adding a environment .env file to ReactJS project. Retrieved from https://www.scien.cx/2022/01/31/adding-a-environment-env-file-to-reactjs-project/

MLA
" » Adding a environment .env file to ReactJS project." Rajesh Royal | Sciencx - Monday January 31, 2022, https://www.scien.cx/2022/01/31/adding-a-environment-env-file-to-reactjs-project/
HARVARD
Rajesh Royal | Sciencx Monday January 31, 2022 » Adding a environment .env file to ReactJS project., viewed ,<https://www.scien.cx/2022/01/31/adding-a-environment-env-file-to-reactjs-project/>
VANCOUVER
Rajesh Royal | Sciencx - » Adding a environment .env file to ReactJS project. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/31/adding-a-environment-env-file-to-reactjs-project/
CHICAGO
" » Adding a environment .env file to ReactJS project." Rajesh Royal | Sciencx - Accessed . https://www.scien.cx/2022/01/31/adding-a-environment-env-file-to-reactjs-project/
IEEE
" » Adding a environment .env file to ReactJS project." Rajesh Royal | Sciencx [Online]. Available: https://www.scien.cx/2022/01/31/adding-a-environment-env-file-to-reactjs-project/. [Accessed: ]
rf:citation
» Adding a environment .env file to ReactJS project | Rajesh Royal | Sciencx | https://www.scien.cx/2022/01/31/adding-a-environment-env-file-to-reactjs-project/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.