Direnv setup for multiple git/GitHub, npm and AWS accounts/credentials
In this post, we’ll look at how to configure direnv to manage multiple git, GitHub, npm and AWS accounts and credentials on a single machine.
Table of Contents
direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current directory.
We can use direnv
to apply credentials and configuration to a subtree of our file system. This is useful for example for freelancers who might have separate clients who give them access to their systems. In this scenario, the configurations shouldn’t affect each and we also wouldn’t want to reset the “global” configuration when changing projects.
The following is an .envrc
file which overrides npm
, gh
, git
and aws
CLI credentials via updating configuration files.
# npm
export NPM_CONFIG_USERCONFIG="~/Documents/project-name/.npmrc"
# GH CLI
export GH_CONFIG_DIR="~/Documents/project-name/.gh-config"
# GH_TOKEN is only needed if you opt to `gh auth login` via token
# export GH_TOKEN="<GH_TOKEN>"
# Git CLI
export GIT_AUTHOR_EMAIL="<find-in-github>@users.noreply.github.com"
export GIT_AUTHOR_NAME="Hugo Di Francesco"
export GIT_COMMITTER_EMAIL="<find-in-github>@users.noreply.github.com"
export GIT_COMMITTER_NAME="Hugo Di Francesco"
# AWS CLI
export AWS_CONFIG_FILE="~/Documents/project-name/.aws/config"
# override the profile to use if not using `default` AWS profile
# from the config using the following
# export AWS_PROFILE="<PROFILE_NAME>"
How to use the .envrc
- Put the above into an
.envrc
at the directory inside of which you want the configuration to be applied - Replace
~/Documents/project-name
with the path to the directory where you will store your configuration (essentially the “home/~
” directory for the configuration you want to use). - Create an
.npmrc
in the right location - Replace “Hugo Di Francesco” with the name you want to commit under.
- Find your
@users.noreply.github.com
email and replace it- the information in Setting your commit email address - GitHub Docs is useful but needs to be adapted
- ensure you select “Keep my email addresses private”
- take note of the
@users.noreply.github.com
- put it into the
.envrc
forGIT_AUTHOR_EMAIL
andGIT_COMMITTER_EMAIL
- Go into the directory containing the
.envrc
, anddirenv allow
it gh auth login
to log into the GitHub CLI- can be checked with
gh auth status
- can be checked with
- Create and configure the
.aws/config
file- can be checked with
aws sts get-caller-identity --no-cli-pager
, see docs
- can be checked with
Further reading
gh
CLI environment variables reference: https://cli.github.com/manual/gh_help_environmentaws
CLI environment variables reference: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html#envvars-listnpm
environment variables guide: https://docs.npmjs.com/cli/v10/using-npm/config#environment-variablesgit
environment variables reference: https://git-scm.com/docs/git#_environment_variablesgit
environment variables reference, “Git Commits” section: https://git-scm.com/docs/git#_git_commits
Get The Jest Handbook (100 pages)
Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library.
orJoin 1000s of developers learning about Enterprise-grade Node.js & JavaScript