Managing multiple versions of node, without NVM or additional tools
So, you are in a situation where you have to use specific versions of Node, but don’t have access to NVM or N.
Its a pain to do the clicky-clicky installer everytime and re-install. You want a quicker way of loading the relevant version.
The crux of this approach is installing two or more different binaries of Node into different folders on your system and then using an alias in your shell to update the path so you can swap between them. A way to point your system to different versions if you will. Another dev (Hi Sam!) gave me the idea and I’m just writing out the detail here for future reference.
Practically, you will be able to enter nr
in your Terminal to switch to Node v22.9.00 for your Node React project, or ns
to switch to an older version of Node to support an older Svelte project that needed v10.9.3, or whatever. Obviously the names, commands, and versions will be particular to you and your projects.
I’m on macOS here so imagine Linux will be similar, not sure on Windows, sorry.
Download the binaries
Head over to https://nodejs.org/dist/ and click on the first version of Node you are interested in. For example, I wanted v22.9.0 so went to https://nodejs.org/dist/v22.9.0/. Then you want to download the relevant binary so get the *.gz file that matches your system. I’m on macOS Apple Silicon, so downloaded https://nodejs.org/dist/v22.9.0/node-v22.9.0-darwin-arm64.tar.gz.
bin
folder. If not, you have likely downloaded the source code by mistake
Now you need to repeat that process for each version you need. For the sake of explanation I have also downloaded node-v10.15.3-darwin-x64.tar.gz.
Move the gz files somewhere practical
I wanted my two (or more) versions of Node stored in folders out of the way but somewhere sensible, so put them within ./local/share/
, so like my v10.15.3 would be in ./local/share/node101503
.
So I moved my downloaded gz files into that share folder ready for the next step. They can go pretty much anywhere on your system, that’s just where I chose.
Extracting the Node binaries
With the gz files for Node versions in the correct folder, we extract the binary to a subfolder with tar -xzf ~/.local/share/node-v10.15.3-darwin-x64.tar.gz -C ~/.local/share/node101503 --strip-components=1
. Obviously adjust the file/folder names for your scenario and repeat for however many versions you need.
Adding a shell alias to switch Node versions
So now we have two or more Node binaries installed on our system, we just need to be able to switch easily between them.
To do this we will add two alias commands to our shell. I’m on zsh so that means editing my .zshrc file.
I added these two new alias commands with other alias’s I have defined, my two aliases for this look like this:
alias ns="export PATH=$HOME/.fzf/bin:$HOME/.local/share/node101503/bin:$HOME/bin:$HOME/.local/bin:/usr/local/bin:$HOME/.local/share/nvim/spec/node_modules/.bin:~/go/bin:$PATH"
alias nr="export PATH=$HOME/.fzf/bin:$HOME/.local/share/node220900/bin:$HOME/bin:$HOME/.local/bin:/usr/local/bin:$HOME/.local/share/nvim/spec/node_modules/.bin:~/go/bin:$PATH"
However, I have other things in there, like a path to Go and other tools that my Neovim config needs. Yours may not need to be that long for each.
The way I understand it, is that you can append more paths to your $PATH
by adding a colon and then the new extra path. So if you only needed to add this new Node path, your export may be as simple as:
alias ns="export PATH=$HOME/.local/share/node101503/bin:$PATH"
alias nr="export PATH=$HOME/.local/share/node220900/bin:$PATH"
Save the file, quit your Terminal or re-source the .zshrc
and try your new command. After each, you can check which Node version with node -v
and check where it is looking for that Node with which node
.
What the path?
By adding more paths to your $PATH
(yes, I know, keep up), your system can look in multiple places for the commands you are asking it to run. Think of it as the equivalent of someone asking “Where is the flat head screwdriver?”, and you can say, “It’s either in my toolbox, or I left it out on the shelf” and the system dutifully goes and checks both locations. I believe that paths near the beginning of the export take precedence over paths near the end too.
Summary
My own workplace rejected using NVM due to security concerns, so while such tools are certainly more convenient, it may not always be practical. This is a bit of work, but when done, you should now have a simple way to swap between Node versions when you need to. The aliases and Node versions are entirely up to you but the good news is you don’t need to use a further tool.
Leave a Reply