Installing Node.js and NPM on Your Local Machine
Node.js and NPM (Node Package Manager) are fundamental tools for many developers today, enabling server-side scripting and the management of package dependencies. This guide will help you install Node.js and NPM on your local machine, covering the major operating systems: Windows, macOS, and Linux.
What are Node.js and NPM?
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It's designed to build scalable network applications. In other words, it's a platform that runs JavaScript outside the web browser.
NPM stands for Node Package Manager. It's the default package manager for the JavaScript runtime environment Node.js. It consists of a command-line client and an online database of public and paid-for private packages, called the npm registry.
Prerequisites
Before we begin, you should have a basic understanding of command line interface (CLI) commands, as you'll need to use them to install Node.js and NPM. It's also useful, though not strictly necessary, to have some basic familiarity with JavaScript.
Installing Node.js and NPM
1. Windows
Via Direct Download:
- Visit the official Node.js website's download page at
https://nodejs.org/en/download/
. - Click on the Windows Installer (.msi) 64-bit or 32-bit download link according to your system type.
- After the download is complete, run the .msi file.
- Follow the prompts in the Node.js Setup Wizard to complete the installation.
Via Package Manager:
For Windows, you can also use package managers like Chocolatey or Scoop.
- Install Chocolatey from
https://chocolatey.org/install
. - Open a command prompt as administrator and run
choco install nodejs
.
Alternatively, with Scoop:
- Install Scoop from
https://scoop.sh/
. - Open a command prompt and run
scoop install nodejs
.
2. macOS
Via Direct Download:
- Visit the official Node.js website's download page at
https://nodejs.org/en/download/
. - Click on the macOS Installer (.pkg) download link.
- After the download is complete, open the .pkg file.
- Follow the prompts in the Node.js Setup Wizard to complete the installation.
Via Package Manager:
For macOS, you can use the Homebrew package manager.
- Install Homebrew from
https://brew.sh/
. - Open Terminal and run
brew install node
.
3. Linux
Via Package Manager:
For Linux distributions, you can use the package manager of your distribution.
Debian and Ubuntu based distributions:
- Open Terminal and update your local package index:
sudo apt update
- Install Node.js with:
sudo apt install nodejs
- Install npm with:
sudo apt install npm
Fedora and other RPM-based distributions:
- Open Terminal and update your local package index:
sudo dnf update
- Install Node.js with:
sudo dnf install nodejs
- Install npm with:
sudo dnf install npm
Via NodeSource:
NodeSource provides more up-to-date versions of Node.js for Linux.
- First, you need to install the PPA in order to get access to its contents. Use curl to perform this operation:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
- Then install the Node.js package:
sudo apt-get install -y nodejs
4. Verification
After the installations, you can confirm the successful installation of both Node.js and npm by checking their versions.
- For Node.js, open your CLI and type
node -v
. This will print the version number, e.g.,v14.17.1
. - For npm, in your CLI, type
npm -v
. This will print the version number, e.g.,6.14.13
.
Working with Multiple Node.js Versions
There are instances when you might need to work with different versions of Node.js for different projects. In such a scenario, using Node Version Manager (NVM) can be highly beneficial. Learn more about working with Node.js versions using NVM in this detailed guide.
Going Beyond: Setting Up MongoDB and Building a MERN Application
If you're looking to further leverage your Node.js and NPM installations, consider setting up a MongoDB database and learning how to build a MERN (MongoDB, Express.js, React, Node.js) stack application. Here are step-by-step guides on installing MongoDB and building a simple event management app with the MERN stack.
Keeping Node.js and NPM Updated
While it's important to get Node.js and NPM installed initially, it's also important to keep them up-to-date. Updated versions often come with performance improvements, new features, and bug fixes.
Updating Node.js and NPM on Windows
If you're on Windows, the easiest way to update Node.js is to download the latest version directly from the official Node.js website and run the installer again. It will replace the old version with the new one.
For updating NPM, open your command prompt and run the following command:
npm install npm@latest -g
Updating Node.js and NPM on macOS
On macOS, if you've initially installed Node.js using Homebrew, you can upgrade it by running the following commands in your terminal:
brew update
brew upgrade node
To update NPM, use the following command:
npm install npm@latest -g
Updating Node.js and NPM on Linux
For Debian and Ubuntu based distributions, use these commands to update Node.js and NPM:
sudo apt-get upgrade nodejs
Updating Node.js and NPM on macOS
On macOS, if you've initially installed Node.js using Homebrew, you can upgrade it by running the following commands in your terminal:
brew update
brew upgrade node
To update NPM, use the following command:
npm install npm@latest -g
Updating Node.js and NPM on Linux
For Debian and Ubuntu based distributions, use these commands to update Node.js and NPM:
sudo apt-get update
sudo apt-get upgrade nodejs
For updating NPM:
npm install npm@latest -g
For Fedora and other RPM-based distributions:
sudo dnf update
sudo dnf upgrade nodejs
And for updating NPM:
npm install npm@latest -g
Uninstalling Node.js and NPM
If you want to uninstall Node.js and NPM from your system, you can do this easily.
Windows
Go to the 'Add or Remove Programs' in your Control Panel, find Node.js in your list of installed software, and click 'Uninstall'.
macOS
If you've installed Node.js via the .pkg file from the Node.js website, you should reinstall it, and then in Terminal run:
sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*
If you've installed Node.js via Homebrew, run:
brew uninstall node
Linux
For Debian and Ubuntu based Linux distributions, use the following command to remove Node.js:
sudo apt-get remove nodejs
For Fedora and other RPM-based distributions:
sudo dnf remove nodejs
To remove NPM, in all the cases above, use the following command:
npm uninstall npm -g
Wrapping Up
Now you know not only how to install Node.js and NPM, but also how to keep them up-to-date and even how to uninstall them if necessary. It's worth noting that Node.js and NPM are active projects with frequent releases, so it's beneficial to keep them updated to get the latest features, improvements, and security patches. Happy coding!
Conclusion
Congratulations! You now have Node.js and NPM installed on your local machine. You can start building your Node.js applications and use NPM for managing your project dependencies. Always ensure to keep them updated to the latest version to access new features and critical security patches.