How to Update Node to Any Version Using NPM

If you are an active Node.js developer, you may hear news about the new release almost every month. You also install these updates on your system to get the new features and bug fixes. There are times when we need to maintain multiple versions of Node with the flexibility to switch between them without going through the hassle of installation.

Switching between various versions is important as all the libraries on NPM are not up to date so it is possible that they require an older version or maybe your project requires a different version for compatibility. 

Well, no worries, in this tutorial I will teach you to update Node.js to any version using NPM. Not only this, you will also get to know how to switch between them. Let’s get started.

Updating to a Specific Node.js Version

There are several ways to update or get a new version of Node.js. Among them, one of the most popular ones is using NVM (node version manager) which we have a guide already on, click here to read it. But for this tutorial, our main focus is on using the easiest way which is by using a super easy-to-implement NPM module named “n”

“n” allows you to switch between different versions of Node.js simultaneously. Here is a detailed GitHub repository on “n”, if you want to go through it in depth: https://github.com/tj/n

Now, let’s jump into the step-by-step guide to installing Node.js different versions and switching between them.

Note:

If you want to use “n”, you will need to have at least one node package manager or npm installed in your system. If you don’t know what node package manager is, click here.

Step 1: Clean NPM Cache

npm cache clean -f

This command is used to clear the cache of the npm and the “-f” flag shows that we are forcing the system to delete the cache. Running this command is useful for freeing up disk space and removing corrupted files. If you are not running this command you end up getting an error to run this.

Note:

These commands are running on a Windows machine (command prompt) so we are executing directly. If you are using Linux or macOS you have to use “sudo” to gain admin privileges. In that case, you can execute commands like “sudo npm cache clean -f”.

Step 2: Install “n” Globally

npm install -g n

This command will install the n module. The “-g” flag instructs it to install in the global node_modules folder, so we can use it directly anywhere.

Step 3: Update Node to Latest or a Specific Version

Once “n” is installed, you can update Node.js to the latest version using the below command:

n stable

If you want a specific version like we needed 8.0.0 then you can do this by running the following command:

n 8.0.0

Step 4: Switch Between Different Versions of Node.js

The “n” module not only helps to install Node but also to manage multiple versions. You can get the latest version or a specific version in use by using this. All you have to do is type n in the terminal and you should see all installed versions listed. Use the arrow keys to choose the version and press enter.

Switch Node Version

Step 5: Get the Current Version Number

Once you switch to a different version of Node.js, you can verify it to avoid any errors during execution. You can use the traditional method of checking its version which is executing the “node -v” command.

node -v

v7.6.0

If you are a big fan of JavaScript – JavaScript Strict Mode (What it Does and Doesn’t Allow)

Conclusion

In this tutorial, we learned about a super cool Node.js package “n”. This gives us a way to install the latest Node.js version or a specific version and also allows us to switch between them, which is really cool. To use this package you just need to install it using NPM and it does not require any additional configuration.

We hope after reading this guide you can update Node to any version you want. I suggest you demo this cool package on your own, so next time you want to use another Nodejs version, use this package for super-fast installation instead of manually installing different versions from the official Node.js website.

Reference

https://stackoverflow.com/questions/7718313/how-to-change-to-an-older-version-of-node-js

Aditya Gupta
Aditya Gupta
Articles: 109