Installing Node.js and managing it's versions were this easy?
A detailed guide about how to install node.js, npm and manage the versions of node in your system
Introduction
We all know that, Node.js is the runtime environment for JavaScript that executes JavaScript code outside the web browser. And, npm stands for Node Package Manager and is the default package manager for Node.js and is also a platform for managing JavaScript packages. It provides a command-line interface (CLI) for interacting with the npm registry, which hosts thousands of open-source libraries and modules.
How to install node.js and npm locally?
Based on the operating systems, the installation process is different and varied. That's why, I'm classifying them here so that you folks don't get confused between them.
Windows
Download the Installer:
Go to the official Node.js website: nodejs.org.
Download the LTS (Long Term Support) version, because it happens to be the most stable version.
Run the Installer:
Open the downloaded
.msi
file and run the installer.Follow the installation steps, and please make sure to check the box that says "Install Node.js and npm."
macOS
You have to install Homebrew if you haven't already. To do that, open the terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Now, we can install
node.js
andnpm
with Homebrew:brew install node
Linux
It's extremely easy to install node.js
and npm
in Linux. Just run these two commands in the terminal:
sudo apt update
sudo apt install nodejs npm
Verify your installation
You can verify the installation of node.js
and npm
by running the following two commands in your terminal:
node -v
npm -v
If you are on Windows, Powershell may cause some issues, so try to use Command Prompt in that case. It will work!!
Managing node versions using NVM
NVM stands for Node Version Manager and it's a command-line tool that helps us to manage and switch different versions of Node.js with ease and convenience!!
Check available node versions
Before installing any node version, let's first check the available Node versions. To do that, we can simply run the following command in our terminal:
nvm ls available
After running this, you get something like this:
Installing the latest node version
To install the latest version of node.js, you can simply run this command,
nvm install latest
But remember, it's always better to install the LTS (long-term support) version of node, because it's less buggy and is overall more stable!!
To install the LTS version of Node, run the command,-
nvm install lts
Install multiple Node versions
One of the most interesting part of NVM is you can install multiple versions of Node at the same time and use any of them based on your convenience!!
For this, nvm has the nvm install
command. You can install specific versions by running this command followed by the version you want. For example,
nvm install 18.17.0
nvm install 20.11.1
nvm install 20.12
Also, as NVM follows semantic versioning, you can install v18.17 and use any of the following version under 18.17, like 18.17.0, 18.17.1, etc. Here, 18 represents the major version, 17 represents the minor version, and 1 represents the patch version!!
Installing specific Node versions
You can install any specific node version, by running this command,-
nvm install <node_version_number>
Replace the <node_version_number>
with your desired Node version.
But, to ensure that your given version is valid, make sure to run nvm ls available
and put a correct version from the list!!
Also, once you install a version of Node, the corresponding version of NPM is also installed alongside with it. So you don’t need to install NPM separately!!
Check installed Node versions in your system
To check the list of all node versions that you have installed on your system, you can simply run,-
nvm list
And, you will see a response like this:
Switching Node versions
As you can see in the previous image that I'm currently using 20.13.1
. Now, if I want to switch my version to another one like 18.17.0
. I can simply use the following command:
nvm use 18.17.0
In your case, you can put your desired version in place of 18.17.0
, but first make sure it is a valid version number and it is installed on your system!!
Uninstall a Node version
To uninstall an already installed Node version that you no longer think is useful, you can do that by running the command,-
nvm uninstall <node_version_number>
Replace the <node_version_number>
with your desired and installed Node version.
Conclusion
TLDR: This article provides a comprehensive guide on installing Node.js and npm across different operating systems (Windows, macOS, and Linux), verifying installations, and managing Node.js versions using NVM (Node Version Manager). It covers steps to install specific Node.js versions, switch between versions, and uninstall versions when no longer needed. The guide emphasizes the importance of using the LTS (Long Term Support) version for stability and includes detailed commands for each process.
Well, that's a wrap for now!! Hope you folks have enriched yourself today with lots of known or unknown concepts. I wish you a great day ahead and till then keep learning and keep exploring!!