Install and switch Node.js versions with nvm
NVM is still the easiest way to install Node.js, switch versions instantly, and migrate global packages when you upgrade.

With NodeJS rapidly releasing newer version since the node community got back on its feet, it is now necessary to have an easy way to maintain your NodeJS installations.
I have tried many different ways to install and maintain Node, distribution packages, binary downloads, building from source and finally using NVM. I know that each method has its advantages and disadvantages, but I now have a favourite.
Nothing beats how easy it is to get to any version of Node using NVM. You can go up, you can go down, heck you can even switch back and forth all the time if that is what you need. Imagine having to run a few hundred tests across major versions of node like most CIs do. Using NVM this is easily done by writing a few lines on a script to switch to the version you need before running each round of tests.
Install nvm
Grab the install script from the current nvm release (version numbers below are examples — check the repo for the latest tag):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
Install and switch Node versions
Then install any version of node simply by writing
nvm install 6.9.1
After you install a few versions of node you can switch between them simply by writing
nvm use 4.5.0
Migrate global packages to a new Node version
But what about your global packages? Say you have a global cli package that you need for you build process. You can either install them each time you install a separate node version or you can again take advantage of nvm and just do
nvm install 6.9.1 --reinstall-packages-from=4.5.2
How is that for easy migration of node versions!? To see what you currently have installed globally before migrating, list them with npm ls -g.
Remove a system-wide Node install
If you now want to remove your system nodejs, since you will probably not need it anymore you can do something along these lines:
sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}
This will delete all files installed by the system wide installed nodejs package. Just make sure you are not messing some other computer account up, as if they don’t use nvm themselves will be left with no node at all
Subscribe to masimplo.com
Get the latest posts delivered right to your inbox

