36

direnv on Windows

 1 year ago
source link: https://gist.github.com/rmtuckerphx/4ace28c1605300462340ffa7b7001c6d
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK