DEV Community

Charles Loder
Charles Loder

Posted on

6

TIDBIT: get direnv to use .env

direnv is a great tool for loading environment variables on a per-project basis.

The one catch is that it loads .envrc files by default and not .env. I found their instructions on how to load an .env a little unclear for a new developer like myself so below I'll show what to do.

I'm assuming:

create a config

First, create a config directory for direnv



mkdir ~/.config/direnv


Enter fullscreen mode Exit fullscreen mode

Then create a config file



touch ~/.config/direnv/direnv.toml


Enter fullscreen mode Exit fullscreen mode

configure

Open the config, and add



[global]
load_dotenv = true


Enter fullscreen mode Exit fullscreen mode

Save, and restart your shell



exec $SHELL


Enter fullscreen mode Exit fullscreen mode

That's it!

Now, when you go to a project with a .env file, you can simply run



direnv allow


Enter fullscreen mode Exit fullscreen mode

It will load the .env instead of just looking for .envrc

I hope this helps someone else!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay