dotenv
is a popular npm package that simplifies the process of loading environment variables from a .env
file into a Node.js application. In this blog post, we will explore how to use dotenv
in a React application written in TypeScript, covering fundamental concepts, usage methods, common practices, and best practices.Environment variables are key - value pairs that are used to configure an application. They can be used to store sensitive information such as API keys, database connection strings, or configuration settings that vary between different environments.
dotenv
is a zero - dependency module that loads environment variables from a .env
file into process.env
in a Node.js application. This allows you to keep your configuration settings separate from your codebase, making it easier to manage and deploy your application in different environments.
React is a popular JavaScript library for building user interfaces, and TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. When using React with TypeScript, we can take advantage of static typing to catch errors early in the development process.
You can use create - react - app
with the TypeScript template to create a new project:
npx create-react-app my-app --template typescript
cd my-app
Install the dotenv
package using npm or yarn:
npm install dotenv
or
yarn add dotenv
.env
fileCreate a .env
file in the root directory of your project. This file will contain your environment variables. For example:
REACT_APP_API_KEY=your_api_key
REACT_APP_API_URL=https://api.example.com
Note that in a React application, environment variables must start with REACT_APP_
to be available in the client - side code.
In your TypeScript code, you can load the environment variables from the .env
file using dotenv
. In the src
directory, you can create a file (e.g., env.ts
) to load the variables:
import dotenv from 'dotenv';
dotenv.config();
export const API_KEY = process.env.REACT_APP_API_KEY;
export const API_URL = process.env.REACT_APP_API_URL;
You can then use these environment variables in your React components. For example, in a functional component:
import React from 'react';
import { API_KEY, API_URL } from './env';
const App: React.FC = () => {
return (
<div>
<p>API Key: {API_KEY}</p>
<p>API URL: {API_URL}</p>
</div>
);
};
export default App;
For different environments (development, production, testing), you can create separate .env
files. For example, .env.development
, .env.production
, and .env.test
. You can then specify which file to use based on the environment. In a Node.js script, you can do something like this:
import dotenv from 'dotenv';
const envFile = process.env.NODE_ENV === 'production' ? '.env.production' : '.env.development';
dotenv.config({ path: envFile });
To avoid type errors when using environment variables, you can define types for them. For example:
interface EnvVars {
REACT_APP_API_KEY: string;
REACT_APP_API_URL: string;
}
const envVars: EnvVars = {
REACT_APP_API_KEY: process.env.REACT_APP_API_KEY as string,
REACT_APP_API_URL: process.env.REACT_APP_API_URL as string
};
export default envVars;
.env
file to version control: The .env
file may contain sensitive information such as API keys and database passwords. Add .env
to your .gitignore
file to prevent it from being committed.if (!process.env.REACT_APP_API_KEY) {
throw new Error('REACT_APP_API_KEY is not set');
}
env.ts
file) to make it easier to manage and update.Using dotenv
in a React application with TypeScript is a powerful way to manage environment variables. It allows you to keep your configuration settings separate from your codebase, making your application more secure and easier to deploy in different environments. By following the common practices and best practices outlined in this blog post, you can effectively use dotenv
in your React TypeScript projects.