DEV Community

Mittal
Mittal

Posted on • Originally published at bosctechlabs.com

Adding an .env file to React Project

Image description

Do you want to know how to add an .env file to react js project? If yes, then this guide can let you understand the process effectively. When using any external APIs for data, you have to add the .env file to store sensitive credentials such as API keys.

The environment variable is beneficial in storing the API link in one location. Therefore it does not require changing the link manually in each file. Now, let’s start adding the .env file to the react js project.

How to add a .env file to React js project?
At first, you have to open the project root directory
Then create the .env file around that directory
After that, open the .env file to declare the environment variable

REACT_APP_TITLE = How to React
Enter fullscreen mode Exit fullscreen mode
REACT_APP_DESCRIPTION = Using .env file in React js
Enter fullscreen mode Exit fullscreen mode

React implements to prefix react_app on the entire custom variable. Variables without the react_app can be ignored. Therefore it is the best choice to create a custom variable like REACT_APP_CUSTOM_VARIABLE.

To print the environment variable, you must put the process.env before the custom variable.

{process.env.REACT_APP_TITLE}
{process.env.REACT_APP_DESCRIPTION}
Enter fullscreen mode Exit fullscreen mode

Now you can start the application npm start and then refresh the browser to check out the changes
This way, you can add the .env file to the React Project.

When working on a large-scale project, you must add separate custom variables for production and development. For instance, you have the live API link for production and the test API link for development, and you don’t need to change that manually in each file.

Therefore, here you have the built-in environment variable known as NODE_ENV. When you write process.env.NODE_ENV and start the server npm start, then it will print the mode you are currently working on. It will print the production for npm build, development for npm start, and then test for npm test.

Now, you can easily switch between the custom variables when you use the if else statement or JavaScript ternary operation to put the condition for the different modes.

For instance, put this in the .env file

REACT_APP_DEV_MODE = This is the development mode
REACT_APP_PRO_MODE = This is the production mode
Enter fullscreen mode Exit fullscreen mode

And then put it in the app.js file.

{process.env.NODE_ENV === 'development' ?
{process.env.REACT_APP_DEV_MODE : process.env.REACT_APP_PRO_MODE}
Enter fullscreen mode Exit fullscreen mode

When you run the npm start, it will print ‘This is a development mode,’ and when you run the npm run build and serve the build folder, it will print ‘This is a production mode.’

Note:
Before the custom variable, always use the react_app prefix
When you add the new environment variable, you need to stop the server and start once again for changes.

Essential process of React JS solutions:
Now in the recent trend, lots of people are using React JS, but you must have some skills in ReactJS before using. Now we will see what is meant by React JS and the uses of the technology. The interoperability and flexibility of React JS make it popular among many people. It helps the programmers develop, deploy and make use to stay connected and provide safe solutions.

Many companies that employ linked services or React JS solutions integrate their systems faster and better, and they may give information everywhere, anytime, on any device. After information and communications are integrated, computing becomes a better experience.

Some of the service providers develop the applications with the help of the React JS architecture. Some of the web application process development is outsourced to lots of less cost locations. The main reason is that it provides the reliability set as the trustworthy for building robust web applications.

The community developers have shown that the web services of the React JS platform are enthusiastic in integrating with conventional technologies. The main benefit of using this technology is that it decreases the cost and time required for the creation and upkeep of commercial applications.

The users have access to necessary information almost from any device. Developing the web application on the React JS platform is more valuable; the other reason is it is recognized as Software interoperability.

Most web admins choose this platform for designing web applications. The interoperability helps the developers make customized solutions by keeping the needs of the business people in mind.

Impact of adding .env file:
The standard programming tasks and the web development are simplified by the environment variables by performing specific tasks. The environment variable is the vital process of the codes used to access the components from the different forms of the .env file.

There are numerous predefined codes that can be used to construct unique procedures of the beneficial methods connected with various types of syntaxes. Making your own.env file is made easier with the aid of syntax general. Other techniques are used to create the.env file, and it can be stored with the process.

To display the data, one can simplify the process of the .env file. Various steps are also available to process the information with the fundamental nature of the essential techniques. To display the data, the environment variable sets up the .env file as the process in the form of an automatic manner.

To format the provided data, it will support that process with the variety of options in the .env file. Paging also supports the operation of specific paging to support navigation movements.

Here for the paging process, there will be a variety of specific functional movements used. The ideal method for advancing through the paging process is first, last, prior. By clicking on the headings in the column, you can sort the results in this area.

Conclusion:
From the scenario mentioned above, now you have got the idea of how to add a .env file to React js project. React experts complete this process without issues. These experts are readily available to give you everything you want. Hence when you want better support, you can trust these experts and get their help.

Frequently asked questions

  1. Why do we use the .env file in React?
    Env files are used to store sensitive credentials such as API keys. An environment variable supports storing the API link at one location so that we do not need to change the Link in each file manually.

  2. What is the custom variable?
    A custom variable is the custom name-value pair assigned to your visitors using a JavaScript Tracking API and visualizes reports of how many visits, conversions, etc.

  3. What is the .env file in React?
    If you are utilizing some of the external APIs for the data, then you must make use of the .env file for saving sensitive information like API keys. Moreover, environment variables also help us to save the Link at any one place and don’t need to change the file manually, a most beneficial feature.

website : https://bosctechlabs.com/add-env-file-in-react-project/

Top comments (2)

Collapse
 
brense profile image
Rense Bakker

No no no no no.

First of it's good to note that using .env like this with React only works out of the box if you created your react project with create-react-app.

Secondly, this is a a hard NO

If you are utilizing some of the external APIs for the data, then you must make use of the .env file for saving sensitive information like API keys.

Values in your your .env that you use in your React app are exposed in your frontend code for anyone to see, you cannot store sensitive data in your frontend. You can only use .env to store sensitive values, when you use it with a NodeJS backend.

You cannot treat anything that you expose to your frontend as "sensitive" at all ever, period. <- thats a 2nd period just to make sure.

Collapse
 
brense profile image
Rense Bakker

Also, because it appears you're writing for a company that offers React consultation. I would like to add that it might be wise to take this article off your companies site. Publishing this article at the very least it might reflect badly on your company and at the worst, some of the clients that you consult for might actually do this and thus expose their secret information through their frontend code, which might lead to law suits and lots of money lost...