.env.local |top| [TOP]
Key Features of .env.local
Framework-by-Framework Deep Dives
How .env.local behaves depends entirely on your toolchain. Let’s look at the three most common scenarios.
2. Separate local for Secrets vs. development for Team Settings
- Commited
.env.development:API_BASE_URL=http://localhost:3000,LOG_LEVEL=debug. - Ignored
.env.local:AUTH0_SECRET=my_super_secret_key,AWS_ACCESS_KEY_ID=AKIA....
If a setting doesn't contain a secret and is the same for every developer, put it in a committed file. Keep .env.local exclusively for things that are unique to your machine.
Common conventions
- File name: .env.local (sometimes .env.development.local for environment-specific frameworks).
- Format: plain text, key=value per line.
- Keep it out of version control (add to .gitignore).
- Use other files for different contexts: .env (base), .env.local (local overrides), .env.production (production), .env.test (test).
- Many frameworks (Create React App, Next.js, dotenv) load these automatically or via dotenv packages.
The Local Developer’s Vault: Understanding .env.local
In the world of modern web development, managing configuration and secrets is a delicate balancing act. You need API keys to test your integration, but you cannot commit those keys to GitHub. You need to toggle features between your machine and the production server, but you don't want to hardcode URLs in your source code. .env.local
Enter .env.local—the unsung hero of the local development environment. It is the bridge between a developer's specific machine setup and the shared codebase.
Loading .env.local in projects
- Node.js with dotenv:
- npm install dotenv
- At app entry (e.g., index.js): require('dotenv').config( path: '.env.local' )
- dotenv-safe for required variables:
- Create .env.example and use dotenv-safe to enforce presence.
- Framework specifics:
- Next.js: supports .env.local, .env.development.local, .env.production.local automatically; variables prefixed with NEXT_PUBLIC_ are exposed to browser.
- Create React App: uses .env, .env.development, .env.production; .env.local is supported and ignored by git by default.
- Vite: supports .env, .env.local, .env.development, and variable prefixes (VITE_ for client exposure).
- Express / custom servers: use dotenv or process.env.
Example Use Case
In a Next.js project, you might have:
-
.env:NEXT_PUBLIC_API_URL=https://api.example.com -
.env.local:NEXT_PUBLIC_API_URL=http://localhost:8000
In this example, when you run your application locally, it will use http://localhost:8000 as the API URL, overriding the default value provided in .env. This way, you can work against a local API without altering the committed configuration.