How Can I Update my NodeJS to the Latest Version? – LinuxWays
Node.js is a JavaScript framework that is used for server-side applications, which mainly includes running web applications outside the client’s browser. Multiple factors make Node.js the best option for such applications, such as it has its lower execution time and better synchronization of code between the client and server. On Linux systems, Node.js can be used as the command line utility to use the npm package manager for the installation of different applications. So, it is necessary to keep the Node.js updated to have better performance, as the updates come with several fixes for bugs in the previous version.
How Can I Update My Node.js to the Latest Version?
Updating Node.js enhances the security and stability of the applications installed in the Linux system, the methods to update the Node.js are:
Method 1: Updating Node.js using Node Version Manager
The node version manager is a utility that is primarily used to install, manage, and update Node.js. Moreover, this utility provides the privilege of installing multiple versions of Node which can be activated or switched to default versions. To update the Node.js using the node version manager, adhere to the following steps:
Step 1: Update the Packages List
It is always advisable to update the packages list of the default package manager of Linux so that further installation of packages is error-free:
Step 2: Install Node Version Manager
The node can be installed via curl or wget command, so to install it through the curl command first install the curl and for that:
Once the curl is installed now install the node version manager through:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
Step 3: Update the Current Shell
To refresh the current shell usually the bashrc file is executed using the source command so in that execute:
To verify the installation of the version manager, check its version using:
Step 4: Install the latest Node Version
To update the node to the latest version first list all the available versions and then find the latest one so for listing the versions of the node execute:
Now scroll down the terminal and the latest version will be at the last in my case the latest version is 21.1.0 so install it using the below syntax:
nvm install <version-number>
Now check the version installed by using the ls command, as it will verify the installation process:
If in case, you want to switch to any other version that is already installed and set as default then execute:
nvm alias default <version-number>
If you just want to change the node version that is currently in use but wants to keep the default version unchanged, then, in that case, execute:
Method 2: Updating Node.js using Node Package Manager
Another way to update Node.js to its latest version is by using the Node Package Manager, which is the package manager for Node.js which is used in frontend JavaScript. So, to upgrade the Node.js to the latest version here are some steps for it:
Step 1: Clean npm Cache
To begin with the process of updating the Node.js first, clean the cache of the package manager as it will delete all the data in its cache folder:
If the npm is not installed then install the node package manager by using the Linux default package manager, and for that execute:
Step 2: Install the n Module Stable Node Version
Now install the stable version of the n module to effectively manage the Node.js by executing:
Step 3: Update the Node.js Version
Once the n module is installed now install the stable version of the node by executing:
sudo -E env “PATH=$PATH“ n stable
To install the latest version, execute:
sudo -E env “PATH=$PATH“ n latest
To install the version of your choice, just place the respective version number in the command below:
sudo -E env “PATH=$PATH“ n <version-number>
Method 3: Updating Node.js using Binary Packages
If you want to install the latest version of Node.js and have removed the previously installed version, then download here are some steps for it:
Step 1: Download the latest Node.js
To download the latest version of Node.js, go to its official website and download it from the download section:
Or execute the below command to download it through the terminal:
wget https://nodejs.org/dist/v21.1.0/node-v21.1.0-linux-x64.tar.xz
Step 2: Extract and install Node.js
Once the file is downloaded, extract the file and install it by executing:
sudo tar -C /usr/local –strip-components 1 -xJf node-v21.1.0-linux-x64.tar.xz
For further verification just verify the version by executing:
Conclusion
To build scalable network applications, Node.js is normally used, which is a JavaScript runtime application. Node.js has compatibility with various platforms and provides rapid data streaming as it saves a lot of time while transferring and uploading files. Therefore, to ensure the proper function of Node.js it is necessary to keep it updated, to update Node.js on Linux there are three ways: by using NPM, by using nvm, and by using the binary packages.