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:\toolsto put thedirenv.exefile 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.exeto download the file - Copy the file to
c:\toolsand 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
.bashrcfile 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:
.envrcwith key and values as follows:
export KEY1=value1
export KEY2="value2"
- From a Git Bash terminal open to the same location as the
.envrcfile, 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
For anyone having difficulty installing direnv on windows. Just use Winget. It worked smoothly for me.
From Command Prompt, just run the command "winget install direnv"
Accept the conditions and it will install automatically afterwards.
Then open a new terminal and check the version with "direnv --version"