Skip to content

Instantly share code, notes, and snippets.

@lmcneel
Forked from rmtuckerphx/direnv-win.md
Created January 12, 2024 18:32
Show Gist options
  • Save lmcneel/58e05cf5bf8a67e2bb687557e06ba6c9 to your computer and use it in GitHub Desktop.
Save lmcneel/58e05cf5bf8a67e2bb687557e06ba6c9 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

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