Learn how to use .env
files in React.js to manage environment variables, protect sensitive data, and streamline your app’s configuration across different environments. Perfect for absolute beginners!
What Are .env
Files?
A .env
(environment) file is a simple text file used to store environment variables—key-value pairs that configure your application’s behavior. These variables define settings like API keys, database URLs, or feature flags, allowing your app to adapt to different environments (e.g., development, testing, production) without code changes.
For example:
REACT_APP_API_KEY=your_api_key_here
REACT_APP_BASE_URL=https://api.example.com
Why Use .env
Files in React?
- Security: Avoid hardcoding sensitive data (like API keys) directly into your code.
- Environment-Specific Configuration: Use different settings for development, testing, or production.
-
Ease of Maintenance: Change configurations without rewriting code—just update the
.env
file. -
Team Collaboration: Share project setups without exposing secrets (e.g., via
.env.example
templates).
How to Create a .env
File in React
-
Create the File:
In your React project’s root directory (wherepackage.json
resides), create a file named:-
.env
for general use, or -
.env.local
for local overrides (ignored by Git).
-
Naming Rules:
React (via Create React App) only loads variables prefixed withREACT_APP_
. For example:
REACT_APP_TITLE=My Awesome App
REACT_APP_DEBUG_MODE=true
Accessing Variables in Your Code
Variables are accessible via process.env
. For example:
// In a React component
const apiKey = process.env.REACT_APP_API_KEY;
function App() {
return (
<div>
<h1>{process.env.REACT_APP_TITLE}</h1>
</div>
);
}
⚠️ Important Notes:
-
Restart Required: After modifying
.env
, restart your dev server (npm start
). -
Build-Time Embedding: Variables are injected at build time. Changes require re-running
npm run build
.
Best Practices for .env
Files
-
Never Commit Sensitive Data:
- Add
.env
to.gitignore
to prevent secrets from being pushed to repositories. - Use
.env.example
(without values) to guide teammates.
- Add
-
Use Environment-Specific Files:
-
.env.development
: Settings for local development. -
.env.production
: Settings for live apps. -
.env.test
: For testing frameworks like Jest.
-
Avoid Frontend Secrets:
React runs on the client side, so any variable in.env
is accessible in the browser. Never store backend secrets (e.g., database passwords) here. Use a backend server for sensitive operations.Validate Variables:
Ensure required variables exist at runtime. Example:
if (!process.env.REACT_APP_API_KEY) {
throw new Error("Missing API key!");
}
-
Keep It Simple:
Don’t overload
.env
with non-environment-specific settings. Use constants or config files for static values.
Key Takeaways
✅ .env
files store environment variables to keep configurations dynamic and secure.
✅ Prefix variables with REACT_APP_
for React to recognize them.
✅ Never commit .env
to version control—use .gitignore
.
✅ Use different .env
files for development, production, and testing.
✅ Client-side apps can’t fully hide secrets—always protect sensitive data on the server.
By mastering .env
files, you’ll build React apps that are secure, flexible, and ready for the real world! 🚀
Top comments (0)