Best way to update & manage your node.js versions

Many a times you face issues with the version of your installed node js. Some packages may ask for latest versions to work smoothly and some others may say they can not work properly with the latest versions. So sometimes the versions of node js really become a headache and you face the requirement to upgrade or downgrade them.

What I do not prefer

There are couple of ways to do that and I’ve found one of the most popular ways people described is by using the npm package n.
But I found it not up to the mark due to the following reasons.

  • Lot of people reported about the package not being stable.
  • Unnecessary requirement for sudo.
  • Slower than nvm.
  • The community is with nvm. Right now nvm has 3 times the stars than n in github.

What I do prefer

I think you already guessed what I want to say. Yes, I think nvm is a better and secure way to update, manage or remove your node js versions. I personally am doing upgrade and downgrade all the times now a days because I work both with ember-cli and angular-cli. Ember-cli wants the node to be below v6.9 and angular-cli wants it above 6.9. So you can guess my situation.

Manage Node js with nvm (Mac/Linux)

Step 1

First of all you need to have nvm (node version manager) installed in your computer. You can run the following curl command to install that.

This should be sufficient to install nvm properly without errors. However if you want to check details or other ways about installation, you can check the document in github.

Step 2

Now that your nvm is installed and ready to be used; install the required version of node.js using nvm in your computer. The command below will so it for you.

You can install multiple versions of node js too.

Step 3

Now that your desired version of node js is installed; you can switch to that using the command below.

The command above will make the current used version of node js in your computer as 7.0.0. To verify it, you can run the version command of node.

Check the installed versions

If any any point you want to see which versions are installed, you can run the following nvm command to list all of them.

Delete a particular version

Even though multiple installed versions will not bother you; as at a time you will be using only one. But if you feel you have some unnecessary versions you can always remove them using the following command.

Update node js to latest version

Many a time you need to install the latest available node.js in your machine. However going to the official node.js website, finding out the latest version and installing and running that using the previously mentioned steps are a little tedious task. That’s why I’ve came up with the following bash function.

All you need to do is to open up your .bash_profile and insert this code snippet over there. Restart your terminal & you will be able to use the command update_nodejs, which will update the node.js in your machine to the latest version.

There is a problem with nvm when using global packages. If you install a package globally and then switch the node js version to some other; you wont be able to access the the global package there. To resolve this, check how to forward global packages in nvm.

About This Author

Hello! I am Paul Shan, a JavaScript Expert, Full Stack and DevOps Engineer cum Consultant based out of Bengaluru, India.