In this article, we will learn how to update Node.js to any version using n, a simple-to-use Node.js version manager. We will also explore how to switch between the installed versions.
The n package is only supported on macOS and Linux devices. If you are on Windows, you can use Windows Subsystem for Linux (WSL), which is very easy to set up. We will also be using WSL for this tutorial.
Why Node.js Upgrades Matter?
New Features: Upgrading to newer Node.js versions allows you to enjoy the latest features and performance improvements.
Compatibility: Many popular frameworks and packages are regularly updated and made compatible with the latest stable version of Node.js. Using an older version may cause compatibility issues and may not support the latest package updates.
Security and Bugs: Newer versions usually fix security issues and bugs, making your applications secure and stable.
Long-Term Support (LTS): Upgrading to LTS versions ensures stability and long-term support, making them a perfect choice for production.
Updating Node.js Version
There are several ways to update Node.js:
- Using NVM
- Using the installer from the official Node.js website
- Using n
Among them, one of the easiest methods is to use the n module. This module lets us download and install the Node.js LTS version, the latest (current) version or any specific version we want.
It also allows us to switch between different versions of Node.js installed on our system simultaneously.
Step 1: Get your current Node.js version
First note down your current Node.js version, so if in future you face any compatibility issue with the latest version, you can get back to your previous version using “n”.
To print the current Node.js version in use, execute the command below:
node -v
Step 2: Choose a version to update
Now decide which version you want to switch to. It may depend on the project you are working on or you can simply upgrade to the latest version.
If you want to upgrade to the latest version, you still have to choose if you want to upgrade to the latest LTS version, this may not be the most recent release, but it is the Node.js latest stable version that provides long-term support. Or you can install the latest version if you want to try out the newly launched features. Let’s see the commands for each of them.
Step 3: Install n Globally
Next, we need to install n to use it. For this, we can use NPM.
npm install -g n
The “-g” flag instructs to install n in the global node_modules folder, so we can use it directly anywhere.
Step 4: Updating Node.js
Once n is installed, you can update Node.js using the below commands.
Update to the latest LTS version:
n lts
Update to the latest version:
n latest
Update to a specific version:
n x.x.x
Replace x.x.x with the desired version number.
How to Switch Node.js Versions?
The n module not only helps to install Node but also to manage multiple versions. We can install different versions and later switch between them. All we have to do is type n in the terminal and all installed versions will listed. Then we can use the arrow keys to choose the version we want and press Enter to switch.
Verification:
Once you switch to a different version of Node.js, make sure to verify it to avoid any errors during execution.
Throughout this article, we have prefixed all commands with sudo because we are using Windows Subsystem for Linux (WSL), where elevated permissions are required. If you are using Linux or macOS, you can avoid using sudo for commands that do not require elevated privileges.
Conclusion
In short, there is a handy package named “n” by which we can install the latest Node.js version or a specific version and switch between them, which is really cool. Sadly “n” is only supported on macOS and Linux devices, not Windows, but you can use WSL for that. At last, for the simplest explanation of any Node.js topic, don’t forget to visit our website!
If you are new to Node.js:
- What is Node.js?
- Asynchronous Programming in Node.js – Callback, Promises & Async/Await
- How to send an HTML File in Node.js?
Reference
https://www.npmjs.com/package/n