How to create env. file in React JS

Cynthia

When working on a React.js project, it's crucial to manage your environment variables effectively. Environment variables store sensitive information, such as API keys, database URLs, and configuration settings, separate from your codebase. This separation enhances security and allows you to switch between different environments like development, staging, and production easily. In this blog post, we'll explore how to create an environment (.env) file in a React.js project to manage these variables.

Why Use an Environment File?

Before diving into the how-to, let's understand why using an environment file is important:

  1. Security: Storing sensitive information like API keys directly in your source code is a security risk. Environment files keep this data separate and safe.

  2. Configuration Management: Different environments often require different configurations. With an environment file, you can easily switch between development, testing, and production settings.

  3. Version Control: You can exclude environment files from version control systems (e.g., Git) to prevent accidentally sharing sensitive data or overriding environment-specific configurations.

Creating an Environment File

To create an environment file in a React.js project, follow these steps:

Step 1: Initialize a React App

If you haven't already, create a new React app using Create React App (CRA) or your preferred setup. Open your project in a code editor.

Step 2: Install dotenv

React doesn't provide built-in support for environment variables, so you'll need to use a package called dotenv. Install it using npm or yarn:

how-to-create-env-file-in-react-js 1

Step 3: Create the .env File

In the root directory of your project, create a file named .env. This file will store your environment variables. For example:

how-to-create-env-file-in-react-js 2

Here's what's happening:

  • Each line defines a variable in the format VARIABLE_NAME=value.
  • Variable names must start with REACT_APP_ in a CRA project to be recognized by Create React App's build process. This prefix ensures that only the variables you explicitly intend to expose are available.

Step 4: Accessing Environment Variables

To access environment variables in your React components or other files, you need to import dotenv and call config() before any other code:

how-to-create-env-file-in-react-js 3

Now, you can access the variables using process.env.VARIABLE_NAME. Make sure to replace "VARIABLE_NAME" with the actual variable name you want to access.

Step 5: Using Environment Variables

You can use environment variables throughout your React app. They are particularly useful for storing sensitive information, such as API keys, like in the example above. Remember that you should not hardcode sensitive data directly into your code.

Ignoring .env Files

For security reasons, you should never commit your .env files to version control. To exclude them, add the following line to your .gitignore file:

.env

This prevents accidentally sharing sensitive information when pushing code to a repository.

Conclusion

Managing environment variables is an essential part of developing secure and flexible React.js applications. By using an environment (.env) file and the dotenv package, you can keep your sensitive information separate from your codebase, easily configure different environments, and enhance the overall security of your application. Remember to never expose sensitive data in your source code, and always follow best practices for managing environment variables.