A zero-configuration React hook for environment variables - the perfect drop-in replacement for process.env
Stop writing process.env.REACT_APP_SOMETHING
everywhere. Get clean, type-safe access to your environment variables with zero setup required.
- π Zero Configuration - Just install and use
- π Drop-in Replacement - Replace
process.env.VARIABLE
withuseEnv()
- π·οΈ TypeScript Support - Full type safety out of the box
- π― Smart Prefix Detection - Works with React, Next.js, Vite, and more
- π§Ή Clean Variable Names - No more prefixes in your code
- β‘ Performance Optimized - Cached for efficiency
- π‘οΈ Error Handling - Built-in validation and helpful error messages
npm install react-env-secrets
yarn add react-env-secrets
pnpm add react-env-secrets
const API_URL = process.env.REACT_APP_API_URL;
const PORT = process.env.REACT_APP_PORT;
const DATABASE_URL = process.env.REACT_APP_DATABASE_URL;
// Lots of repetitive process.env.REACT_APP_ everywhere! π«
import { useEnv } from 'react-env-secrets';
function MyComponent() {
const { API_URL, PORT, DATABASE_URL } = useEnv();
return <div>API URL: {API_URL}</div>;
}
That's it! No configuration needed. π
import { useEnv } from 'react-env-secrets';
function App() {
// Get all your environment variables at once
const { API_URL, PORT, DEBUG_MODE } = useEnv();
return (
<div>
<h1>My App</h1>
<p>API: {API_URL}</p>
<p>Port: {PORT}</p>
<p>Debug: {DEBUG_MODE}</p>
</div>
);
}
import { useEnv } from 'react-env-secrets';
function App() {
const env = useEnv({
required: ['API_URL', 'DATABASE_URL'], // Will show helpful errors if missing
fallbacks: {
PORT: '3000',
DEBUG_MODE: 'false'
}
});
return <div>API: {env.API_URL}</div>;
}
import { useEnvVar } from 'react-env-secrets';
function ApiComponent() {
const apiUrl = useEnvVar('API_URL', 'http://localhost:3000');
return <div>Connecting to: {apiUrl}</div>;
}
import { useEnv, type EnvVars } from 'react-env-secrets';
interface MyEnvVars extends EnvVars {
API_URL: string;
PORT: string;
DEBUG_MODE?: string;
}
function App() {
const { API_URL, PORT, DEBUG_MODE } = useEnv() as MyEnvVars;
// Full type safety! β
}
Create a .env
file in your project root:
# React apps
REACT_APP_API_URL=https://api.example.com
REACT_APP_PORT=3000
REACT_APP_DEBUG_MODE=true
# Next.js apps
NEXT_PUBLIC_API_URL=https://api.example.com
NEXT_PUBLIC_PORT=3000
# Vite apps
VITE_API_URL=https://api.example.com
VITE_PORT=3000
# Or custom prefix
PUBLIC_API_URL=https://api.example.com
The hook automatically detects and strips these prefixes:
REACT_APP_
NEXT_PUBLIC_
VITE_
PUBLIC_
So REACT_APP_API_URL
becomes just API_URL
in your code! π―
Main hook for accessing environment variables.
Parameters:
options
(optional): Configuration objectprefix?: string
- Custom prefix filterrequired?: string[]
- Required variable namesfallbacks?: Record<string, string>
- Fallback values
Returns: Object with all environment variables
Hook for a single environment variable.
Parameters:
key: string
- Variable name (without prefix)fallback?: string
- Fallback value
Returns: Variable value or fallback
Clears the internal cache. Useful for testing.
Development helper to see all available variables.
// This is what we're all tired of writing:
const API_URL = process.env.REACT_APP_API_URL;
const API_KEY = process.env.REACT_APP_API_KEY;
const PORT = process.env.REACT_APP_PORT || '3000';
const DEBUG = process.env.REACT_APP_DEBUG === 'true';
// Repetitive, verbose, and error-prone! π€
// Clean, simple, and powerful:
const { API_URL, API_KEY, PORT, DEBUG } = useEnv({
fallbacks: { PORT: '3000' }
});
// That's it! π
- Client-Safe: Only exposes prefixed environment variables on the client
- Server-Compatible: Works seamlessly in server-side rendering
- Development Warnings: Helpful error messages when variables are missing
- Type Safety: Catch environment variable issues at compile time
Works perfectly with:
- β Create React App
- β Next.js
- β Vite
- β Remix
- β Gatsby
- β Any React framework
import { clearEnvCache, listEnvVars } from 'react-env-secrets';
// Clear cache between tests
beforeEach(() => {
clearEnvCache();
});
// See available variables in development
console.log(listEnvVars());
We welcome contributions! Please see our Contributing Guide for details.
MIT Β© Rayyan Waseem
- π Documentation
- π Issue Tracker
- π¬ Discussions
Made with β€οΈ for React developers who are tired of process.env.REACT_APP_EVERYTHING