HomeBlogDashboardUsageAboutProjects
// end of file

Thanks for scrolling all the way down.

Written after midnight, somewhere in Singapore.

GitHubLinkedInTwitter
Ru Chern · Software Engineer · 2026
--:--:--SGT · SINGAPORE
21 Apr 2026 · 5 min read

direnv: load per-project environment variables with .envrc

Set up direnv in zsh to load per-project environment variables from .envrc, reuse an existing .env file, and keep production databases out of reach.

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.

What direnv does

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.

Install direnv and hook it into zsh

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.

Create your first .envrc

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.

The folder decides where commands go

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.

Share values between projects

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.

Load an existing .env file with direnv

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

Keep .envrc secrets out of git

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.

Add a project bin folder to PATH

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.

When direnv is not loading

If nothing happens when you cd in, check these in order:

  • The hook is missing. Run direnv status. If it does not report a loaded .envrc, check that the hook line is in ~/.zshrc and that you restarted the shell.
  • The file is blocked. A new or changed .envrc needs direnv allow before it runs.
  • A file it reads changed without a reload. direnv watches .envrc and any file loaded with dotenv. For changes anywhere else, run direnv reload.

Takeaways

  • Secrets stay scoped to the project that needs them.
  • The folder you are in decides which database and server your commands reach.
  • The 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.