Lesson 1.2: Installing Node.js and npm (Node Package Manager)
Table of Contents 1. What is npm?2. Installation Steps3. Updating Node.js and npm4. Installing Packages with npmSummary1. What is npm? npm (Node Package Manager) is a tool that comes with Node.js and allows you to download and manage third-party libraries and packages for your project. It’s an essential part of working with Node.js as it […]
1. What is npm?
npm (Node Package Manager) is a tool that comes with Node.js and allows you to download and manage third-party libraries and packages for your project. It’s an essential part of working with Node.js as it gives you access to thousands of open-source libraries that can help with everything from server setup to data manipulation.
2. Installation Steps
Step 1: Downloading Node.js
- Visit the Node.js website: Go to https://nodejs.org/.
- Choose the Right Version: You’ll see two options:
- LTS (Long Term Support): Recommended for most users; it’s stable and well-supported.
- Current: Includes the latest features but may be less stable.
- Download the Installer: Click on the appropriate download link for your operating system (Windows, macOS, or Linux) and save the installer file.
Step 2: Installing Node.js on Different Operating Systems
- Windows:
- Open the downloaded
.msi
file. - Follow the setup wizard, and allow Node.js to make changes to your computer.
- Ensure the “Add to PATH” option is checked so you can access
node
andnpm
commands from the command line. - Finish the installation.
- Open the downloaded
- macOS:
- Open the downloaded
.pkg
file. - Follow the installation steps, similar to installing other macOS applications.
- Complete the installation.
- Open the downloaded
- Linux:
- On most Linux distributions, you can install Node.js via the package manager. Here’s an example for Ubuntu:
sudo apt update
sudo apt install nodejs npm
Step 3: Verifying the Installation
After installing Node.js and npm, open your terminal or command prompt and run the following commands to verify the installation:
- Check Node.js version:
node -v
This command should return the installed version of Node.js (e.g.,v16.0.0
). - Check npm version:
npm -v
This command should return the installed version of npm (e.g.,7.0.0
).
3. Updating Node.js and npm
Over time, you may need to update Node.js and npm to access new features or bug fixes.
- Updating Node.js:
- You can download the latest version from https://nodejs.org/ and reinstall it.
- Alternatively, use n (a package for managing Node versions) if you have npm installed:
npm install -g n sudo n stable
- Updating npm:
- Run the following command to update npm to the latest version:
npm install -g npm
- Run the following command to update npm to the latest version:
4. Installing Packages with npm
Once you have Node.js and npm installed, you can install libraries and packages easily. For example, to install the popular Express.js library, run: npm install express
Summary
In this lesson, we covered how to install Node.js and npm on different operating systems and how to verify the installation. Understanding npm and keeping Node.js updated will be essential for working effectively with Node.js.