Switching Node Versions Without nvm

Instantly, automatically switch node versions, with .nvmrc compatibility, without any terminal start-up delay.

Daniel Young
1 min readDec 9, 2019

I have recently been working on two parallel node projects which, for reasons we won’t go into, are exclusively compatible with different versions of node.

I began using nvm to switch node versions. Automatically switching node versions based on .nvmrc files is great, but I found that nvm initialisation introduced a noticeable delay in starting new terminal windows. As my workflow is essentially terminals and browsers this was unacceptable, so I endeavoured to create a simpler solution.

The basic idea is to pre-install required node versions and symlink to the desired version as needed.

node-switch.sh — install node versions and switch between them

Thankfully the package names back to version 0.10.0 follow the same naming convention; see https://nodejs.org/download/release/v12.13.1/ . You’ll want to switch to tar.gz URLs if you going back prior to v4 though (the script currently downloads the tar.xz package).

Also be sure to change the system variable at the top of the script to match your OS.

With the script saved in $HOME/scripts/node-switch.sh and an alias:

alias ns=$HOME/scripts/node-switch.sh

We can now do the following:

ns install 12.13.1
ns switch 12.13.1

This is combined with a stripped-down version of the .nvmrc switching logic to maintain compatibility with project directories using.nvmrc.

The result is an instantly responsive node version switcher which doesn’t delay terminal start up. It’s nowhere near as comprehensive as nvm (bash completion, version look-up, etc) but it’s working well so far.

--

--