Skip to content

Instantly share code, notes, and snippets.

@rmtuckerphx
Last active January 12, 2024 18:32
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rmtuckerphx/4ace28c1605300462340ffa7b7001c6d to your computer and use it in GitHub Desktop.
Save rmtuckerphx/4ace28c1605300462340ffa7b7001c6d to your computer and use it in GitHub Desktop.
Steps to install direnv on Windows

direnv on Windows

Overview

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.

Steps to install

For these steps, it is assummed that you have installed Git Bash on Windows. I also use VSCode as my editor.

  1. Create a folder such as c:\tools to put the direnv.exe file and add it to the Windows PATH
  2. Go to https://github.com/direnv/direnv/releases to find the latest release of direnv.
  3. Click on direnv.windows-amd64.exe to download the file
  4. Copy the file to c:\tools and rename to direnv.exe
  5. Open the Git Bash terminal and execute $ echo ~ to find the location in Windows of the user's home directory
  6. If a .bashrc file doesn't exist in the user's home directory, create one
  7. Follow the instructions at https://direnv.net/docs/hook.html#bash and add the following text to .bashrc: eval "$(direnv hook bash)"
  8. In your node project directory, create a file: .envrc with key and values as follows:
export KEY1=value1
export KEY2="value2"
  1. 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
  1. 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

@janpio
Copy link

janpio commented May 3, 2022

First start of Git bash after following these instructions output "WARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or ~/.profile." and then created ~/.bash_profile automatically.

@g-hamilton
Copy link

Create a folder such as c:\tools to put the direnv.exe file and add it to the Windows PATH

Can you please advise exactly how to complete this step, assuming I follow the same file and folder structure in your example?

I can't figure out how to get the PATH setup correctly.

My .bashrc file includes only the line:

eval "$(direnv hook bash)"

.. and when I start a new Git Bash terminal I get the error:

bash: direnv: command not found

@majidaldo
Copy link

Create a folder such as c:\tools to put the direnv.exe file and add it to the Windows PATH

Can you please advise exactly how to complete this step, assuming I follow the same file and folder structure in your example?

I can't figure out how to get the PATH setup correctly.

My .bashrc file includes only the line:

eval "$(direnv hook bash)"

.. and when I start a new Git Bash terminal I get the error:

bash: direnv: command not found

winget install direnv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment