There's some nice tooling around for managing nvm, such as zsh-nvm. However, I have found this tooling somewhat insufficient, or having poor performance.
For my needs, all I want is to increase the performance of my shell startup, and to run nvm use
automatically whenever I navigate around the filesystem.
To these ends, I have written a very simple solution that one can include in their dotfiles. nvm
is initially loaded as a shim that will load nvm
when it is run, vastly decreasing my shell's initialisation time (from ~5 seconds to 0.2 seconds). For users of zsh, a chpwd
hook is added which will check for a .nvmrc
file in the new current directory, and run nvm use
if one is found. If the specified version of node.js is not installed, nvm simply prints a message telling you to run nvm install
instead - it will not do this for you. A check is also included to run on initialisation, if the shell is opened immediately into a folder containing an .nvmrc
file.
Most of this functionality is cross-shell compatible. It's only the automatic calling of nvm use
that depends on zsh. Other shells may have equivalent functionality to zsh hooks that you could port it to.