Automatic Version Switching with NVM
April 25, 2020
I'm a big fan of using NVM for managing my Node version. For personal projects, I like to use the most current version, and generally bump it at the earliest opportunity. However, I have a number of projects (particularly older Ember projects) at work that have hard dependencies on older versions.
Normally, there are a few ways to use NVM to switch Node versions as needed. For a simple version switch, I could simply enter nvm use 13.0.0
.
For something a little more automated, I can create a .nvmrc
file containing the node version in my project's root, and simply cd
into the folder and enter nvm use
.
It would be nice to have something even more brainless though. Enter Zsh hooks! If you use Zsh, you can set up a hook that automatically makes the switch for you every time you cd
into a folder that contains a .nvmrc
file.
To set this up, simply add this snippet to your .zshrc
, anywhere after you initialize NVM:
# NVM automation | |
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 |
This hook makes using NVM completely hands-off. When you switch to a new folder, you'll automatically get switched to the version specified by that folder's .nvmrc
.
If the file specifies a Node version that you don't have installed, it will get installed.
If the folder doesn't contain this file, you'll get switched to the default Node version.
The main disadvantage to this approach is that you need Zsh for this to work. Maybe this will be the encouragement you need to finally make the switch.