Created
November 5, 2024 02:18
-
-
Save channprj/6b48da34998deb6f6e38a133914d4644 to your computer and use it in GitHub Desktop.
direnv for Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Append in ~/.direnvrc below: | |
use_node() { | |
layout node | |
local node_version=$1 | |
if [ "$node_version" == "package.json" ]; then | |
# Requires jq: `brew install jq` | |
if ! command -v jq &> /dev/null; then | |
echo -e "\033[0;31mjq is not installed. Please type 'brew install jq'.\033[0m" | |
return 1 | |
fi | |
node_version=`jq --raw-output .engines.node package.json | tr -d "<=> "` | |
fi | |
# Load node version from n | |
local node_path=${NODE_DIR:-/usr/local/n/versions/node} | |
if [ -x "$node_path/$node_version/bin/node" ]; then | |
PATH_add $node_path/$node_version/bin | |
elif [ -x "$node_path/v$node_version/bin/node" ]; then | |
# Support nvm users | |
PATH_add $node_path/v$node_version/bin | |
else | |
echo -e "\033[0;31mNode.js version $node_version is not installed. Plase type 'sudo n install $node_version'\033[0m" | |
fi | |
export NODE_PATH=./node_modules | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use node version specified in package.json like below: | |
# "engines": { | |
# "node": ">=20.15.1" | |
# }, | |
use node package.json | |
# ...or use specific version | |
use node 22.11.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment