In JavaScript projects, I used to use dotenv so that I could put local environment variables in a .env
file for local development. But dotenv requires you to add code to your project.
With direnv, you can put local env vars in a .envrc file and those env vars are loaded automatically in the shell.
For these steps, it is assummed that you have installed Git Bash on Windows. I also use VSCode as my editor.
- Create a folder such as
c:\tools
to put thedirenv.exe
file and add it to the Windows PATH - Go to https://github.com/direnv/direnv/releases to find the latest release of direnv.
- Click on
direnv.windows-amd64.exe
to download the file - Copy the file to
c:\tools
and rename todirenv.exe
- Open the Git Bash terminal and execute
$ echo ~
to find the location in Windows of the user's home directory - If a
.bashrc
file doesn't exist in the user's home directory, create one - Follow the instructions at https://direnv.net/docs/hook.html#bash and add the following text to
.bashrc
:eval "$(direnv hook bash)"
- In your node project directory, create a file:
.envrc
with key and values as follows:
export KEY1=value1
export KEY2="value2"
- From a Git Bash terminal open to the same location as the
.envrc
file, enter the command:direnv allow
$ direnv allow
direnv: loading /c/dev/myproject/.envrc
direnv: export +KEY1 +KEY2
- To test that the values are loaded:
$ echo $KEY1
value1
$ echo $KEY2
value2
NOTE: after a change to the .envrc
, you must again execute direnv allow