Guide
How to manage multiple .env files: local, staging and production
One project, three environments, three sets of variables. Here are the common ways to switch between them, what each one costs you, and a way that doesn’t leave secrets lying around.
By Anmol Sethi · Updated
01
The setup
Most projects end up with one file per environment next to the code:
.env # what the app actually reads
.env.local
.env.staging
.env.productionYour app reads .env. The question is how the right values get there, and how you know which ones are active.
02
Copy the file over
cp .env.staging .envSimple and works with every tool. But nothing tells you which environment is active later, and any edit you made to .env is overwritten without a word.
03
Point .env at one file with a symlink
ln -sf .env.staging .env
ls -l .env # shows which file it points toNow ls -l answers “which one is active?”. The catch: editing .env edits .env.staging itself, and every environment still sits on disk as a plain-text file.
04
Let the framework pick
- Vite:
vite --mode stagingloads.env.stagingon top of.env. - Next.js: loads
.env.developmentfornext devand.env.productionfornext build/next start, plus.env.localin both. - Laravel:
php artisan serve --env=staging(orAPP_ENV=staging) loads.env.stagingif it exists.
Great when your framework supports it, but each one has its own rules, and none of them help with other tools in the same project.
05
Load a file for one command
The dotenv-cli package runs any command with a given file loaded, leaving .env alone:
npx dotenv-cli -e .env.staging -- npm startHandy for one-off runs. You still keep every environment as a file, and you have to remember the flag every time.
06
Side by side
| Method | Shows what’s active | Secrets as files | Works with any tool |
|---|---|---|---|
cp | No | Yes | Yes |
| Symlink | ls -l .env | Yes | Mostly |
| Framework modes | Per framework | Yes | No |
dotenv-cli | Per command | Yes | Yes |
| Dotvane | Menu bar, colour-coded | Only the active .env | Yes |
07
The one-click way: Dotvane
Dotvane imports .env and every .env.<name> as profiles stored in your Keychain. Pick one in the menu bar and .env is rewritten, readable by you alone. The bar turns red on production, hand edits are caught before they’re lost, and dotvane run staging -- npm start does the per-command trick too.
Try Dotvane free for 7 days. Every feature, no account.
Download for Mac