Skip to content

Instantly share code, notes, and snippets.

@pascaliske
Last active June 29, 2021 09:27
Show Gist options
  • Save pascaliske/aa72478787ac9498ae8aa3fb33bed06d to your computer and use it in GitHub Desktop.
Save pascaliske/aa72478787ac9498ae8aa3fb33bed06d to your computer and use it in GitHub Desktop.
Integrate `tj/n` with `direnv`

Integrate tj/n with direnv

This gist explains how to setup direnv together with tj/n. When a node version isn't available at the moment the envrc file gets loaded the node version will be downloaded automatically. Happy coding! ☕

Step 1: Install direnv

Use Homebrew to install the direnv tool.

brew install direnv

Copy file direnvrc below into ~/.config/direnv/direnvrc for integrating tj/n.

Tip: You can globally git ignore all .envrc files:

echo ".envrc" >> ~/.gitignore

Step 2: Create .envrc with current node version

# create file
cat <<EOF > .envrc
#!/usr/bin/env bash
# -*- coding: utf-8 -*-

use node $(node -v)
layout node
EOF

# allow file initially
direnv allow
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
export DIRENV_WARN_TIMEOUT=30s
function _exit() {
if [[ $1 != "" ]]; then
echo "$1"
fi
/bin/kill -INT $$
}
function _viewHelp() {
local COMMAND=${1}
local ARGUMENTS=${2}
local DESCRIPTION=${3}
echo "$DESCRIPTION"
echo ""
echo " usage:"
echo " $ $COMMAND $ARGUMENTS"
_exit
}
function use_node() {
local VERSION="$1"
# display help
if [[ $VERSION == '' ]]; then
_viewHelp "use node" "<version>" "Changes the binary path to the given node version using the n version manager."
fi
# install if not already installed
if n bin "$VERSION" >/dev/null; then
:
else
n --quiet "$VERSION"
fi
# set node path
export PATH="$(dirname "$(n bin "$VERSION" | awk '{$1=$1};1')"):$PATH"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment