You can recognize Node.js files by the extension “.js”. They contain actions that are to be performed during certain events, and an example of an event would be an attempt to access a server port. Despite its compelling advantages, Node.js is most effective when it’s up to date. There are updates every couple of weeks to help boost security and stability. Keeping your Node.js up to date is vital to protecting your applications.
The procedure for updating Node.js depends on the environment you are running it in, so we’re taking a look at the update process on Linux, Windows, and macOS.
|Visit WP Engine|
Three methods of updating Node.js on Linux
The Linux world is diverse and features dozens of distributions. So the actual steps for running an update of Node.js on Linux will depend on the distribution. That said, the steps we cover here will work for the major distributions, including CentOS, Debian, and Ubuntu. There are three ways to do an update.
1. Updating Node.js on Linux using Binary Packages
This is the most tedious method of updating Node.js. There may be instances when it is the only option at your disposal, so it’s important that you understand how to do it. For this method, first, visit the official Node.js download page to obtain the 64-bit or 32-bit Linux binary file. You can download the file by using a browser or the console.
Run the following command:
wget https://nodejs.org/dist/vx.x.x/node-vx.x.x-linux-x64.tar.xz — (where x.x.x is the version of the binary file)
You will require to have xz-utils to unpack this file. To install xz-utils, run the following command:
sudo apt-get install xz-utils
Next, install the package in usr/local by running:
sudo tar - C /usr/local --strip-components 1 -xJf node-vx.x.x-linux.x64.tar.xz
With both npm and Node.js installed with binary packages, you can use the Node Package Manager method below to run the next Node.js update.
2. Updating Node.js on Linux using Node Package Manager
Using Node Package Manager (npm) to update Node.js is a more straightforward method than using binary packages. Node.js comes with npm installed by default.
To update Node.js with npm, start by checking what version of npm you currently have by running: npm -v.
Next, execute the command “npm install npm@latest -g” and install the most current npm update. Run “npm -v” once again to confirm that the update occurred.
For the Node.js update, you will require the n module. Run the following code:
- sudo npm cache clean -f - To clear npm cache.
- sudo npm install -g n - Install the n module.
- sudo n stable - Obtain the most current stable Node.js version.
Alternatively, use “sudo n latest” to get the most current version or “sudo n x.x.x” for a more specific Node.js version (where x.x.x is the version you want).
3. Updating Node.js on Linux using Node Version Manager
Node Version Manager (nvm) is often considered the best method. You must have a C++ compiler, libssl-dev code packages, and build-essential.
Commence by updating the code packages repository via the command:
sudo apt -get update
Update nvm itself by running either of the following where x.x.x is the nvm version (if you do not already have curl, install it by running “sudo apt install curl”):
- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/vx.x.x/install.sh | bash
- wget -q0- https://raw.githubusercontent.com/nvm-sh/nvm/vx.x.x/install.sh | bash
Run the following code to restart the terminal for the system to be aware of the changes:
Confirm the successful update of nvm by running the following instruction to check the current version:
After that, check what Node.js version is currently installed by running “nvm ls” and confirm what is the latest release available from the public repository by executing the command “nvm ls-remote”.
Download and compile the most current Node.js version by running “nvm install x.x.x” where x.x.x is the Node.js version number.
You can instruct nvm what version it should use in every new shell via the command “nvm use x.x.x”.
You can define a default version using the alias “nvm alias default node”.
Windows and macOS systems Node.js update
As proprietary environments, Windows and macOS do not suffer from the diversity of Linux distributions. Before you commence, the version of Node.js you are running by keying in the command “node -v”. There are two ways you can run a Node.js update of macOS and Windows.
1. Updating using macOS and Windows installers
You can find binary packages for macOS and Windows on the official Node.js page. However, that would be an unnecessarily complicated process. Instead, you could go around this step using pre-developed macOS (.pkg) and Windows (.msi) installers that are available on the Node.js page. Installers speed up and simplify the process.
To update Node.js, download the macOS or Windows installation file from the Node.js website. Thereafter, allow the user-friendly setup wizard to guide you through the installation process.
Accept the license agreement, choose the installation folder and select the Node.js features you want. Click Finish or Close once the installation process has been completed.
With the completion of each Node.js update, the new version replaces the older one. Confirm that you have the current Node.js version with the command “node -v”.
2. Updating using Node Package Manager
You can also update Node.js on macOS and Windows by using npm. Overall, the process is not all that different from that on Linux. You simply use the n model with npm command. This facilitates your interaction with different versions of Node.js.
Start by checking what version of Node.js you are currently using by executing the command:
Next, clear the npm cache:
npm cache clean -f
Install n as a global package:
npm install -g n
Use the n module to update Node.js to the most current version with the command:
sudo n latest
or choose a specific version by running:
sudo n <version>
Node.js has grown in popularity partly thanks to its improved efficiency in handling requests compared to alternatives such as ASP or PHP. In addition, it runs as a single process and does not form distinct threads for different requests.
For instance, when ASP and PHP handle a file request, both wait for the computer file system to open and read the file before returning the content to the client. Only then are ASP and PHP ready to act on the next request. With Node.js, there is no wait. The request is sent to the computer file system, and Node.js is immediately ready to act on the next request. Once the file system opens and reads the file, the server returns the content back to the client.
It’s recommended that you run the latest version (or the latest stable version) to get the best performance out of Node.js and consistently enjoy its features. We have outlined best practices for downloading and updating Node.js on Linux, Windows, and macOS. Irrespective of the environment, you can use Node.js default package manager, npm. For Linux, your other options are nvm and binary packages, while for Windows and macOS, you should go with their respective Node.js installers.