Introduction
When developing applications with Node.js, developers often need additional tools and libraries to complete their projects. For example, an application might require Express.js to create a web server or a testing library to check whether its code works correctly.
Installing and managing these packages manually can take time. This is where npm (Node Package Manager) becomes useful.
npm helps developers install, update, remove, and manage packages in Node.js projects. It also provides tools to run project scripts and manage dependencies.
What Is npm?
npm stands for Node Package Manager. It is the standard package manager commonly used with Node.js.
npm consists of three main parts:
- npm website: A place to discover packages published by developers.
- npm Registry: An online database where packages and their metadata are published and distributed.
- npm CLI: A command-line tool used to install and manage packages in a project.
For example, if you want to build a web server using Express.js, you can install it with a single command:
npm install express
npm downloads Express.js and its required dependencies and makes them available to your project.
npm is commonly installed alongside Node.js, so you generally do not need to install it separately.
Why Is npm Used?
npm makes package management easier and helps developers organize their Node.js projects.
The main uses of npm include:
- Installing packages: Add libraries and tools to your application.
- Managing dependencies: Keep track of the packages required by your project.
- Updating packages: Install newer versions when needed.
- Removing packages: Delete packages that are no longer required.
- Running scripts: Execute commands for development, testing, building, and other tasks.
- Sharing packages: Publish reusable code that other developers can install.
- Managing project versions: Use package.json and package-lock.json to describe dependencies and record resolved package versions.
For example, a Node.js developer can use npm to install Express.js, run a development script, and manage the project’s dependencies without manually downloading each library.
How to Install npm
npm is normally installed automatically when you install Node.js.
Step 1: Download Node.js
Visit the official Node.js website:
https://nodejs.org/
Download and install a suitable Node.js release for your operating system. For most beginners, the LTS (Long-Term Support) release is a good starting point.
Step 2: Check the Installed Versions
Open Command Prompt, PowerShell, or a terminal and run:
node -v
This command displays the installed Node.js version.
Next, run:
npm -v
This command displays the installed npm version.
If both commands return version numbers, Node.js and npm are available in your terminal.
Note: If either command is not recognized, check that Node.js is installed correctly and that its installation directory is included in your system’s PATH.
What Is package.json in npm?
The package.json file contains important information about a Node.js project, including its name, version, scripts, and dependencies.
It helps npm understand which packages the project needs and which commands can be executed.
You can create a package.json file by running:
npm init
This command asks you several questions about your project.
To create the file using default answers, run:
npm init -y
For example, a simple package.json file might look like this:
{
"name": "my-node-app",
"version": "1.0.0",
"description": "My first Node.js project",
"main": "index.js",
"scripts": {
"start": "node index.js"
}
}
The name property identifies the project, version specifies its version, and scripts defines commands that can be executed through npm.
As you install packages, npm can also add dependency information to this file.
What Is package-lock.json?
The package-lock.json file records the exact resolved versions of the packages installed for a project, along with information about their dependency relationships.
It helps developers and teams install consistent dependency trees across different environments.
For example, when you install Express.js, npm normally updates package.json and creates or updates package-lock.json.
Important: Keep package-lock.json in version control for most application projects. It helps other developers install the same resolved dependency versions.
Common npm Commands
Here are some important npm commands that Node.js developers use regularly
| Command | Purpose |
|---|---|
npm -v |
Displays the npm version. |
npm init |
Creates a package.json file interactively. |
npm init -y |
Creates a package.json file using default values. |
npm install |
Installs project dependencies listed in the project files. |
npm install express |
Installs Express.js as a project dependency. |
npm install -D nodemon |
Installs Nodemon as a development dependency. |
npm uninstall express |
Removes Express.js from the project dependencies. |
npm update |
Updates installed packages within the version ranges allowed by the project. |
npm list |
Displays installed packages. |
npm run |
Lists available npm scripts. |
npm start |
Runs the start script when one is defined. |
Let’s explore some of these commands in more detail.
1. npm init
The npm init command initializes a Node.js project by creating a package.json file.
Example:
mkdir my-node-app
cd my-node-app
npm init -y
This creates a project directory and generates a package.json file with default values.
You can now add dependencies and scripts to the project.
2. npm install
The npm install command installs packages required by a project.
For example, to install Express.js, run:
npm install express
After installation, npm adds Express.js to the project’s dependencies and places the installed package in the node_modules directory.
You can then use it in your Node.js application.
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Welcome to TechFliez!");
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Save this code in index.js and run:
node index.js
Open http://localhost:3000 in your browser to see the response.
3. npm install -D
Some packages are needed only during development, such as tools for automatically restarting an application or checking code quality.
Use the -D option to install a package as a development dependency.
Example:
npm install -D nodemon
Nodemon can restart your Node.js application when it detects file changes.
You can add a script to package.json:
{
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
}
Then start the development server using:
npm run dev
This runs Nodemon, which watches your application files and restarts the server when they change.
4. npm uninstall
The npm uninstall command removes a package from your project.
For example:
npm uninstall express
This removes Express.js from the installed dependencies and updates the relevant project files.
Use this command when your application no longer needs a package.
5. npm run
npm allows you to define scripts in the package.json file and execute them from the terminal.
For example
{
"scripts": {
"start": "node index.js",
"test": "node --test"
}
}
You can run the scripts using:
npm start
Or:
npm test
The commands executed depend on the scripts defined in your project. The test example uses Node.js’s built-in test runner and requires compatible test files.
What Is the node_modules Directory?
The node_modules directory contains the packages installed for a Node.js project, including the dependencies those packages require.
For example, after running:
npm install express
npm creates or updates the node_modules directory with Express.js and its required packages.
This directory can become large, so developers generally do not commit it to Git. Instead, they commit package.json and package-lock.json and run npm install to restore the dependencies when setting up the project.
Difference Between npm and npx
npm and npx are related tools, but they serve different purposes.
| npm | npx |
|---|---|
| Primarily manages packages and project scripts. | Executes package-provided commands. |
| Commonly used to install and uninstall packages. | Commonly used to run a package command without manually installing it globally. |
| Example: npm install cowsay | Example: npx cowsay “Hello” |
If the package is not already available locally, npx may fetch it temporarily to execute the command. Review unfamiliar packages before running them.
Modern npm installations include npx.
Common Mistakes When Using npm
Beginners may encounter the following problems when working with npm:
- Forgetting to initialize the project: Run npm init -y when you need to create a new package.json file.
- Running commands in the wrong directory: Make sure the terminal is open in the correct project folder.
- Ignoring dependency files: Keep package.json and package-lock.json synchronized and commit them to version control.
- Installing every package globally: Install project-specific dependencies locally unless a global installation is genuinely required.
- Using outdated packages without checking: Review package versions, compatibility, and security advisories before updating dependencies.
- Deleting node_modules without reinstalling: If you remove the directory, run npm install to restore the project dependencies.
Conclusion
npm is an essential tool in modern Node.js development. It helps developers install packages, manage dependencies, run project scripts, and share reusable code.
Understanding commands such as npm init, npm install, npm uninstall, and npm run will help you manage Node.js projects more effectively.