Carles Andres' avatarHomeBlog
Back to blog

Load environment variables automatically

Dir env

I have recently discovered direnv, a tool that allows you to load environment variables automatically based on the current directory.

This is the TL;DR of how to set it up.

Installation

bash

Add this to your ~/.zshrc:

bash

Configure your project

In your project, create a .envrc file and make sure it's ignored by git. The file needs to follow the right format:

You could use other .env formats but it will probably require other tools to make their content available to direnv.

Give permissions

In your project's folder run:

bash

Load the vars

In your project's folder run:

bash