NVM Auto Use
Managing Node versions is a rite of passage, and around here at GoodFit we lean on NVM to keep things tidy.
If you haven't installed it yet, you can summon it with:
brew install nvm
Once that's squared away, drop the snippet below into your shell config. It keeps an eye out for a .nvmrc file whenever you hop between directories and seamlessly switches to the right Node version.. no more manual nvm use rituals 😌
NOTE: Ensure this snippet is placed after nvm has been initialised.
# NVM Auto Use
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
Once this is in place, just roam your project directories and let your shell magically keep your Node universe aligned. 🧙🏻♂️