Running a specific version of Node
One of the biggest pains of working with Node is when either a library you're working with is pinned to a specific version of Node or you must pin your project to a specific version because one or more dependencies will not work in versions after the specified one (or so the developers of the library say).
This can happen in one of two ways.
- The supported versions are included in the
engines
section of thepackage.json
file as an advisory - are included in a
.nvmrc
file
Even though neither of these options will stop you from running the package with an unsupported version, then it's on you to fix any potential problems.
The solution #
Out of laziness, I use nvm, as my Node Version Manager.
NVM helps me keep from having to manually install each version of Node that I want to keep around and makes sure that the packages of each version will not conflict with another
When researching possible solutions, I came across this code, taken from this stack overflow answer.
The idea is simple:
If there is a .nvmrc
file in the directory we change to then do the following:
- Run the specified version
- If the version is not installed, then install it
If the .nvmrc
file is not present then the script will switch to the Node version indicated by the Node variable in NVM.
The script will do the following:
- Capture local variables
- Capture the current version of Node running in nvm
- Capture the path to the
.nvmrc
file if one exists
- If there is a
.nvmrc
file in the current directory, set the localnvmrc_node_version
to the Node version specified in.nvmrc
- If the required version is not installed, then use nvm to install it
- If it is installed then use
nvm use
to run that specific version - If there is no
.nvmrc
file in the current directory then use the default installed Node version - Add the
load-nvmrc
function to thechpwd
hook - Run the function to initialize it
The script looks like this:
load-nvmrc() {
# 1
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
# 2
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
# 3
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
# 4
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
# 5
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
# 6
add-zsh-hook chpwd load-nvmrc
# 7
load-nvmrc
If we put this script in the account's .zshrc
file, the script will be available