Categories
React Answers

How to use dotenv in a React project?

Spread the love

Sometimes, we want to use dotenv in a React project.

In this article, we’ll look at how to use dotenv in a React project.

How to use dotenv in a React project?

To use dotenv in a React project, we create our React project with Create React App.

Then we can access environment variables from process.env.

We run

npx create-react-app my-app
cd my-app
npm start

to create the my-app project with create-react-app.

Then we can put our environment variables in an .env file.

And then we can access the variable with

const BASE_URL = process.env.REACT_APP_BASE_URL;

We can access any environment variable in .env that starts with REACT_APP_.

Conclusion

To use dotenv in a React project, we create our React project with Create React App.

Then we can access environment variables from process.env.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *