Understanding .env files with ReactJS

Find out how to really use the .env file with React or NextJS so you never make a mistake again.
•
Updated on August 14, 2026
•
3 min read
Believemy logo

What is a .env file?

It’s a file used to store environment variables.

Good to know

An environment variable is simply a variable that can be accessed throughout the entire project.

It’s generally used to:

  • Store API keys
  • Remember certain URLs
  • Make certain names configurable (such as the site name)

 

Why use a .env file?

The main benefit is that if a developer wants to modify an API key, an address, or even a name, they only need to change it in one place: the .env file.

Without a .env file, the developer would have to modify the API key in all the files where it’s used. This could take hours upon hours.

With this file, the information is changed in only one place, the .env file.

Another benefit is that, by default, the .env file is not sent to remote services such as GitHub or GitLab: the .env file is ignored by Git.

Question

Wait, what is Git? 👀

Git is a command-line software (used from the computer's terminal) that allows you to create multiple versions of a project.

It is an extremely important tool for any good web developer.

Good to know

Want to learn Git?

👉 Learn Git with our course by clicking here

In short, there’s no risk of uploading secret keys online because the file containing our information is never pushed online. This ensures adherence to security principles and avoids redundancy.

 

How to create a .env file?

In the root of your project, such as a React project, simply create a .env file with no name—just this extension.

You can also create specific files depending on the context in which your project is running.

For example:

  • .env.development - the file used in development
  • .env.production - the file used in production

 

How to use a .env file?

Creating a new variable

To use a .env file, start by creating a new variable.

To do so, open the .env file and type this:

DOTENV
GOOGLE_API_KEY=value
Warning

There is no need to add keywords before a variable. However, uppercase letters are used by convention.

 

Accessing a variable

The technique depends on whether you used Vite to create your React project or Create React App.

JSX
// With VITE
const apiKey = import.meta.env.GOOGLE_API_KEY;

// With Create React App / Next.js
const apiKey = process.env.GOOGLE_API_KEY;

 

Example of using a .env file

Let’s take an example of making a request.

JSX
const apiKey = import.meta.env.GOOGLE_API_KEY;
const apiUrl = import.meta.env.GOOGLE_API_URL;

const fetchData = async () => {
    const response = await fetch(apiUrl, {
        headers: {
            "Authorization": `Bearer ${apiKey}`,
            "Content-Type": "application/json"
        }
    });

    if (response.ok) {
        console.log(await response.json());
    } else {
        console.error(`Error: ${response.status}`);
    }
};

export default fetchData;

In this small example, we use the variables GOOGLE_API_KEY and GOOGLE_API_URL to store:

  • our API URL
  • our API key

Easy, right? 👀

 

Conclusion

We’ve seen how to use a .env file with React. Keep in mind that it’s very useful to use one.

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

How to use localStorage in a NextJs or React application

The aim of this article is to teach you how to use localStorage in a Next.JS or React application. Indeed, there are a few differences with the normal use, in a vanilla application.

December 11, 2024

How to install TinyMCE in a project using React or NextJS?

Find out how to install TinyMCE in React or NextJS and fully customize it with plugins to match your projects.

December 6, 2024

Send an email with NextJS and SendGrid for free

Have you always wanted to send emails with JavaScript? Or more specifically, how to send emails with React or NextJS? Then let's find out together how to use SendGrid! It's a quick and easy solution to set up that will help us ensure that our emails are sent and, most importantly, received.

December 9, 2024

Creating a markdown previewer with React

Creating a real markdown previewer with React, and above all, doing it correctly, requires real concentration! Fortunately, with a little help anyone can do it. Here are a few tips for creating your own markdown previewer!

December 9, 2024

Implementing over-request revalidation with NextJS

If you're a web developer, you probably know how important it is to have a fast, responsive site to deliver an optimal user experience. In this article, we're going to show you how to set up on-demand revalidation with NextJS, a feature that refreshes your site's data only when necessary. Using this method, you can save resources and improve the performance of your website, while offering up-to-date data to your users.

December 9, 2024

Setting up a loader when changing a Nextjs page

Have you ever been on a site where you click on a link and nothing happens? Or perhaps you've wondered whether your request was actually received? This is where having a loader comes in. In this article, we'll explain why it's important to have a loader on your website, how to set it up in your Nextjs project and create a function that will display the loader until the page is loaded. If you want to improve the user experience on your website and reassure your visitors, this article is for you.

December 11, 2024

Setting up SCSS themes and contexts with NextJs or React

You can't modify a scss variable, because scss converts its code to css, applying the variable values directly. This makes it difficult to set up a light/dark theme on your React or Next project and scss. Fortunately, there are solutions, which we'll be exploring in this article.

December 11, 2024

Code faster with React using these techniques

Learn how to code faster with React thanks to simple but effective techniques used by experienced developers.

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.