I work across a handful of projects in a day. Each one has its own database URL, its own API keys, and its own idea of which server to talk to. Sourcing .env files by hand is tedious, and exporting secrets into my global shell is how a command ends up pointed at the wrong database.
direnv loads per-project environment variables from an .envrc file when I cd into a project, and unloads them when I leave.
direnv hooks into your shell. Every time you change directory, it looks for an .envrc file in that directory or above it. If it finds one, and you have allowed it, direnv runs the file and loads the result into your current shell.
Leave the directory, and those variables are gone. No manual unset. No stale DATABASE_URL from the last project.
On macOS with Homebrew:
brew install direnv
Then hook it into your shell. For zsh, add this to the end of ~/.zshrc:
eval "$(direnv hook zsh)"
For bash, use ~/.bashrc and direnv hook bash. Restart your shell.
Drop an .envrc into a project:
export DATABASE_URL="postgres://localhost:5432/blog"
export NODE_ENV="development"
cd into the directory and direnv refuses to run it until you approve:
direnv: error .envrc is blocked. Run `direnv allow` to approve its content
Run direnv allow. From then on, every time you enter that directory, those variables are exported. Leave, and they are unset.
The approval is the safety layer. If the .envrc changes, whether you edited it, a teammate did, or a branch switch brought in a different version, direnv blocks it again until you approve the new content.
This is the part I care about most.
In this blog's repository, .envrc points DATABASE_URL at a development branch of the database, not production. It also points my command line tools at the local server instead of the live site.
So any command I run inside that folder, whether a migration, a seed script or an ingest, talks to development. To reach production, I have to say so explicitly. The safe target is the default because of where I am standing, not because I remembered.
If several projects need the same values, put them in an .envrc in a parent folder, and pull it in from each project's first line:
source_up_if_exists
source_up_if_exists looks for an .envrc in the parent folders and loads it. If there is none, nothing happens. The project's own values come after, so they win.
One caution. direnv's own documentation notes that a file loaded this way is not checked by the approval step. Only the project's .envrc is. Keep the parent file in a folder only you write to.
Many projects already have a .env file that other tools read. You do not need to copy the values. .envrc can load it:
dotenv
direnv reads .env and exports everything in it. If the file does not exist, direnv reports an error. Use dotenv_if_exists when the file is optional, or pass a path to load a different file:
dotenv_if_exists .env.local
Decide first whether your .envrc holds secrets. The answer changes what you do with it.
If it holds secrets, which mine do, never commit it. Ignore it in every repository at once with your global gitignore:
.envrc
.direnv/
That is Git's default global ignore file. If you have set core.excludesFile, use that file instead. git config --global core.excludesFile prints it.
If it holds no secrets, only dotenv_if_exists .env.local and tooling setup, commit it so teammates get the same setup. Put the secrets in .env.local and ignore that. Leave .envrc out of your global ignore in this case, or Git will skip it when you commit.
The .envrc is a shell script, so it can do more than export variables. The one I use is PATH_add:
PATH_add bin
That puts the project's ./bin at the front of PATH, only while you are inside the project.
If nothing happens when you cd in, check these in order:
direnv status. If it does not report a loaded .envrc, check that the hook line is in ~/.zshrc and that you restarted the shell..envrc needs direnv allow before it runs..envrc and any file loaded with dotenv. For changes anywhere else, run direnv reload.direnv allow gate means you approve every change before it runs.If you move between projects that point at different databases, let the folder make that choice for you. It is one less thing to remember on a tired afternoon.