How to switch between different Node.js versions on Linux
Hi again. Few days ago, I tried to install a node.js framework and ended up with an error saying that my current node version did not fit the minimum requirements. Updating was not an option since it would break the compatibility with the project I'm currently working on. There must have been a solution for such issues, and I've made a small research and with to share the results. The issue sounds like quire typical and hope the following is going to be useful.
For fast switching between different versions of Node.js, there's an application called NVM (Node Version Manager). To install it, open the console and paste the following:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash
At the moment, the latest NVM version is v.0.29.0. To make sure that you install the latest version, go to the project's GitHub page and check the description – there should be the installation command to copy from.
To start using nvm command, you need either re-login to the system or type source ~/.profile.
You also must have installed Git, and if you still haven't, type sudo apt-get install git or sudo yum install git (whichever command your Linux distro uses).
So now typing nvm doesn't return “command not found”? Great. Let's go ahead.
To see what node.js versions are currently available, type:
At the moment of writing, the latest version is v5.0.0, which was just released. Let's say, you want to download it to check the latest features but keep using v.4.2.2 (this is a long type support version) for your other projects. To download them, use the following commands:
nvm install 5.0.0 nvm install 4.2.2
To see, which node.js versions you have already available, type:
If you had any node.js installed before installing nvm, it would be marked as “system”.
Okay, and the most important part, how to actually make the switch. It's really easy. To start using v5.0.0, type:
nvm use 5.0.0
And to make sure that it worked, check the current node's version:
node -v // v5.0.0
To go back to using another version of node, type nvm use 4.2.2 or nvm use system.
The last issue you might want to fix is that terminal may be forgetting the selected node version, and you have to type nvm use every time you need to use node. In this case, you need to set a default version by typing the following:
nvm alias default 4.4.7
Now each time you access the terminal, you'll have node 4.4.7 ready to go.
This was a brief introduction to NVM. Hope, it's been of help to you, and now you know how to change your system's node.js version with one simple console command.