Leveraging Environment Variables in React: A Safe & Effective Guide
Shrihari
Posted on October 6, 2023
Using environment variables in React applications is crucial for maintaining security, flexibility, and a clean separation between development and production environments. In this article, we'll explore what environment variables are, why they're beneficial, and how to harness their power in a React application.
What are Environment Variables?
Environment variables are external values that can be accessed and consumed by your application but aren't directly hard-coded into your source files. They are particularly useful for:
- Storing sensitive information like API keys, ensuring they aren't exposed in the frontend code.
- Configuring settings between different environments, like development, staging, and production.
- Keeping configuration separate from code, aiding in scalability and maintenance.
Setting Up Environment Variables in Create React App (CRA)
If you've initialized your React project using Create React App (CRA), integrating environment variables is simple.
1. Creating .env files:
In the root directory of your project, you can create files named:
- .env: Default.
- .env.development: For the development environment.
- .env.production: For the production environment.
Inside these files, you can define environment variables in the format:
REACT_APP_MY_VARIABLE=value
Note: CRA mandates that custom environment variables start with REACT_APP_ to ensure they are safe to expose in your JS code.
2. Accessing Variables in the App:
Once defined, you can access these variables in your React components using process.env:
console.log(process.env.REACT_APP_MY_VARIABLE);
3. Using Variables in the Public Folder:
If you need to reference an environment variable in the public folder, say in the index.html, you can do so like this:
<title>%REACT_APP_MY_VARIABLE%</title>
Using Environment Variables Outside of CRA:
If you're not using CRA, you might be leveraging a custom setup with Webpack or another bundler. In this case, you can use the dotenv and webpack.DefinePlugin packages.
1.First, install dotenv:
npm install dotenv
- In your Webpack configuration, require the package and use the DefinePlugin:
const dotenv = require('dotenv').config().parsed;
//...
plugins: [
new webpack.DefinePlugin({
'process.env.MY_VARIABLE': JSON.stringify(dotenv.MY_VARIABLE),
}),
]
Now, your environment variables from the .env file are accessible in your React code via process.env.
Best Practices:
Never Commit .env with Sensitive Data: Ensure that .env files containing sensitive data (like API keys) are added to .gitignore. You don't want these files pushed to public repositories.
Use .env.local for Local Overrides: If you need local-specific values, CRA supports an .env.local file that overrides the closest .env.
Variable Naming: Be descriptive with environment variable names. REACT_APP_API_ENDPOINT is clearer than REACT_APP_ENDPOINT.
Fallback Values: Always have default values in your code for essential environment variables in case they're missing.
Conclusion:
Environment variables offer a robust way to manage configuration and sensitive data in React applications. Whether you're using Create React App or a custom setup, understanding and utilizing environment variables will make your applications more secure, maintainable, and scalable. As always, remember to keep sensitive data out of your repositories and always ensure your environment variables are appropriately set up for all environments, from development to production.
Posted on October 6, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.