Paramclasses Academy - Blog

Tutorials Details

←
Post 7 of 40
→

Understanding npm: Install, Update and Manage Packages in Node.js

Understanding npm: Install, Update and Manage Packages in Node.js

October 29, 2026

✦ Understanding npm: Install, Update and Manage Packages in Node.js

When developing applications with Node.js, you often need additional tools and libraries to perform tasks such as building web servers, connecting databases, validating forms, and sending emails. Instead of writing everything from scratch, you can use ready-made packages created by other developers.

This is where npm (Node Package Manager) becomes important. npm helps you install, update, remove, and manage packages in your Node.js projects.

In this practical tutorial by Param Classes, you will learn what npm is, how to initialize a Node.js project, install packages, manage dependencies, update libraries, and use important npm commands with examples.

✦ What Is npm in Node.js?

npm stands for Node Package Manager. It is the default package management tool distributed with Node.js installations.

npm provides access to a large registry of reusable JavaScript packages. Developers can install these packages in their projects and manage the dependencies required by their applications.

For example, if you want to create a web application using Express.js, you can install Express through npm instead of manually downloading and configuring its files.

❖ Why Is npm Important?

npm helps developers:

  • Install third-party packages.

  • Manage project dependencies.

  • Update packages when new versions are available.

  • Remove packages that are no longer needed.

  • Run project scripts such as development and testing commands.

  • Share reusable packages with other developers.

  • Keep track of package versions using project configuration files.

✦ How to Check Node.js and npm Installation

Before using npm, make sure Node.js is installed on your computer.

Open Command Prompt, PowerShell, or a terminal and run:

node -v

This displays the installed Node.js version.

Next, check npm:

npm -v

If both commands display version numbers, you can proceed with your project.

If either command is not recognized, install Node.js using the official website: https://nodejs.org/

npm is normally included with the standard Node.js installation.

✦ Step 1: Create Your First npm Project

First, create a folder for your application.

mkdir npm-demo
cd npm-demo

Now initialize the project:

npm init -y

The npm init command creates a package.json file. The -y option accepts the default answers automatically.

Your project structure will look similar to this:

npm-demo/
└── package.json

The package.json file contains project information, scripts, and dependency declarations.

For example:

{
  "name": "npm-demo",
  "version": "1.0.0",
  "description": "Learning npm with Node.js",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "license": "ISC"
}

Your generated file may contain slightly different values. You can edit these fields according to your project's requirements.

✦ Step 2: Install a Package Using npm

Let's install Express.js, a popular framework for creating Node.js web applications.

Run this command inside your project folder:

npm install express

You can also use the shorter command:

npm i express

After installation, npm creates or updates the following items:

  • node_modules/ — contains installed packages and their dependencies.

  • package.json — records the declared project dependency.

  • package-lock.json — records the resolved dependency tree and package versions.

Your project structure will look similar to this:

npm-demo/
├── node_modules/
├── package.json
├── package-lock.json
└── app.js

The exact contents of node_modules depend on the packages installed.

❖ Use the Installed Express Package

Create an app.js file:

const express = require('express');

const app = express();
const PORT = 3000;

app.get('/', (req, res) => {
    res.send('Hello from npm and Node.js!');
});

app.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}`);
});

Run the application:

node app.js

Open http://localhost:3000 in your browser.

You should see:

Hello from npm and Node.js!

This example demonstrates how npm installs a package and how your application imports and uses it.

✦ Step 3: Understand Dependencies in package.json

When you install a package using npm install, npm normally adds it to the dependencies section of package.json.

For example:

{
  "dependencies": {
    "express": "^5.1.0"
  }
}

The exact version depends on what npm installs when you run the command.

Dependencies are packages required by your application at runtime.

For example, if your application uses Express to handle web requests, Express is a runtime dependency.

❖ Install a Development Dependency

Some packages are useful during development but are not required to run the application in production.

Examples include testing tools, formatters, and development utilities.

Install a development dependency using:

npm install --save-dev nodemon

The shorter form is:

npm i -D nodemon

Development dependencies are recorded under devDependencies.

For example:

{
  "devDependencies": {
    "nodemon": "^3.1.0"
  }
}

The displayed version is illustrative; your installed version may differ.

You can use Nodemon to restart a Node.js application automatically when you change files.

Add a script to package.json:

{
  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js"
  }
}

If your existing package.json has other fields, keep them and add or update the scripts section.

Run the development server:

npm run dev

✦ Step 4: Update npm Packages

Packages receive updates for bug fixes, improvements, and security patches. npm provides commands to check available updates and install newer versions.

❖ Check Outdated Packages

Run:

npm outdated

This command displays installed packages for which newer versions are available, along with version information.

If no packages need updating, the output may be empty.

❖ Update Packages

Run:

npm update

This updates installed packages within the version ranges permitted by your project's dependency declarations.

It does not necessarily install the newest major version if that version falls outside the declared range.

To inspect the available versions of a particular package, use:

npm view express versions

Before updating packages in an important project, review the release notes, check compatibility, and run your tests.

❖ Install a Specific Package Version

If you need a particular version of a package, specify it explicitly:

npm install express@5

This requests the latest version matching the specified major version available to npm at installation time.

For repeatable deployments, commit your package-lock.json file and use:

npm ci

This installs dependencies from the lockfile and is especially useful in automated builds and continuous integration environments.

✦ Step 5: Remove a Package

If you no longer need a package, you can remove it using npm.

For example:

npm uninstall express

This removes Express from the project's installed dependencies and updates the dependency declarations and lockfile.

To remove a development dependency, use the same command with its package name:

npm uninstall nodemon

Removing a package may break your application if other parts of your code still depend on it. Check your imports and test the application afterward.

✦ Important npm Commands for Beginners

Command Purpose
npm init -y Initialize a project
npm install Install project dependencies
npm install express Install Express
npm install -D nodemon Install a development dependency
npm update Update packages within allowed version ranges
npm outdated Check for available package updates
npm uninstall express Remove a package
npm list Display installed dependency information
npm list --depth=0 Display top-level installed packages
npm run List available package scripts
npm run dev Run the configured development script
npm ci Install dependencies from the lockfile

✦ Common npm Errors and Their Solutions

❖ 1. npm Is Not Recognized

Possible cause: Node.js or npm is not installed correctly, or the installation directory is missing from your system's PATH.

Solution: Verify the installation, reopen your terminal, and check node -v and npm -v. Reinstall Node.js if necessary.

❖ 2. Package Not Found

Possible cause: The package name is misspelled or the package cannot be found in the configured registry.

Solution: Check the package name and registry configuration. You can inspect the configured registry with:

npm config get registry

❖ 3. Dependency or Version Conflicts

Possible cause: Two packages require incompatible versions of a dependency.

Solution: Review the error details, check package compatibility, and use compatible versions. Avoid forcing installation with options such as --force without understanding the consequences.

❖ 4. Application Cannot Find a Module

Possible cause: The required package is not installed in the current project or the import path is incorrect.

Solution: Confirm that you are running the command from the correct project directory and install the missing package if required.

npm install

This installs dependencies declared in the project's package files.

✦ npm Security Best Practices

Managing packages responsibly is important for secure Node.js development.

  1. Install packages from trusted sources.

  2. Review dependencies before adding them to important applications.

  3. Run npm audit to identify known vulnerabilities in the dependency tree.

  4. Review audit findings before applying fixes.

  5. Keep package-lock.json under version control for application projects.

  6. Avoid exposing authentication tokens or private credentials in project files.

  7. Test the application after dependency updates.

You can inspect known dependency vulnerabilities with:

npm audit

The command reports vulnerabilities identified by the configured audit data. It does not guarantee that your application is completely secure.

✦ Frequently Asked Questions (FAQs)

❖ 1. What is npm in Node.js?

npm is the package management tool used to install and manage JavaScript packages and project dependencies.

❖ 2. Is npm installed with Node.js?

npm is normally included when you install Node.js using the standard installer.

❖ 3. What is the purpose of package.json?

It stores project metadata, scripts, and dependency declarations, among other configuration details.

❖ 4. What is the difference between dependencies and devDependencies?

Dependencies are packages needed by the application at runtime. Development dependencies support tasks such as testing, formatting, and local development.

❖ 5. What is package-lock.json?

It records the resolved dependency tree and package versions to help produce consistent installations.

❖ 6. What is the difference between npm update and npm install?

npm install installs requested packages and reconciles the project's dependencies. npm update updates installed packages within their permitted version ranges.

❖ 7. Can I use npm with Express.js?

Yes. You can install Express using npm install express and import it into your Node.js application.

✦ Conclusion

npm is an essential tool for modern Node.js development. It allows you to install packages, manage dependencies, update libraries, run scripts, and organize project configuration through files such as package.json and package-lock.json.

By practising these commands, you can prepare for more advanced development topics, including Express.js, REST API development, MySQL integration, authentication, and full-stack JavaScript applications.

At Param Classes, learners can explore practical programming concepts and develop their backend development skills step by step.

Visit Param Classes for programming tutorials and ParamWebInfo for website development and digital solutions

←
Post 7 of 40
→