NPM & Package Management

If you have started learning Node.js, one of the first confusing terms you will hear is NPM. Every tutorial says “install this package using npm,” “run npm install,” or “check npm version,” but nobody explains what’s happening behind the scenes. 


Npm (Node Package Manager) is the default nodejs package manager that comes with Node.js. It helps you install, manage, and update reusable code libraries called NPM packages. Instead of writing everything from scratch, you use packages NPM provides for authentication, database connection, validation, APIs, and more. 


In this blog, we will learn: 


  • What is Node Package Manager?

  • What NPM packages actually are?

  • How does NPM installation work?

  • How to use NPM in real projects?

  • The basics of versioning

What is NPM?

npm (Node Package Manager) is the official NodeJS package manager that comes bundled with Node.js. When you install Node.js in your system, you get NPM built in along with it. In reality, NPM has three main roles: 


  • Registry: A huge online database of open-source npmjs packages (millions of them)

  • CLI Tool: A command-line tool to install, update, and manage NPM packages

  • Dependency Manager: It keeps track of the libraries your project depends on


Imagine building an API. For that, you need: 


  • Express server

  • Database connector

  • Authentication system

  • Input validation

  • Environment variable management


Instead of coding all this manually, you just install Node JS NPM packages like this: 

npm install express


And that’s it. Npm downloads the package and adds it to your project. 

What is a Package in Node.js?

A package in Node.js is reusable JavaScript code that solves a specific problem. Instead of writing everything from scratch, developers use npm packages to speed up development. For example, if you want to create a web server, you don’t build it naturally. You install a ready-made package: 

npm install express


Then use it in your project: 

const express = require('express');

const app = express();


app.listen(3000);


That’s it. All these npmjs packages are stored in the npm registry, and you install them using the npm node package manager. 

How to Use NPM with Node.js?

Using npm node package manager is simple. Just follow these steps: 

Step 1: Initialize a Project

First, create a new folder and initialize NPM: 

npm init -y


This creates a <package.json> file. The file tracks your project name, version, and all installed npm packages. 

Step 2: Install a Package

To install any nodejs NPM packages, use: 

npm install express


This does three things: 


  • Downloads the package

  • Creates a <node_modules> folder

  • Adds it to <package.json>

Step 3: Install as a Development Dependency

For tools like nodemon: 

npm install nodemon --save-dev


Now, it will appear under <devDependencies>

Step 4: Run Scripts

Inside <package.json>, you can define scripts: 

"scripts": {

  "start": "node index.js"

}


Run it using: 

npm start

Using an NPM Package in the Project

Once you install an npm package, you import it and start using it in your code. 


After running: 

npm install express


You can use it inside your project like this: 

const express = require('express');

const app = express();


app.get('/', (req, res) => {

  res.send('Server is running');

});


app.listen(3000, () => {

  console.log('Running on port 3000');

});


That’s how node js npm packages work. 


Install > Import > Use


Most npm JavaScript packages are used in one of two ways: 


  • <require()> (CommonJS, traditional Node.js)

  • <import> (ES modules, modern syntax)


Example with ES Modules: 

import express from 'express';


One important habit beginners must build is to always check: 


  • Official documentation

  • Version compatibility

  • Required configuration


Because different versions of npmjs packages may work differently. 

Popular NPM Packages for NodeJS

Here are some widely used Node JS NPM packages that every beginner should know: 


Package Name

Purpose

Install Command

express

Web framework for building APIs & servers

<npm install express>

mongoose

MongoDB object modeling

<npm install mongoose>

dotenv

Manage environment variables

<npm install dotenv>

nodemon

Auto-restarts the server during development

<npm install nodemon --save-dev> 

bcrypt

Password hashing

<npm install bcrypt>

jsonwebtoken

JWT authentication

<npm install jsonwebtoken>

cors

Enable cross-origin requests

<npm install cors>

axios

HTTP client for APIs

<npm install axios>


These npm packages are commonly used in backend development, MERN stack projects, and real-world applications. 

Versioning in NPM

When you install npm packages, you will notice something like this inside <package.json>: 

"dependencies": {

  "express": "^4.18.2"

}


That <^4.18.2> is the version number. 


npm follows Semantic versioning (SemVer): 

MAJOR.MINOR.PATCH


Example: 

4.18.2


  • 4: Major version (big changes)

  • 18: Minor version (new features add-on)

  • 2: Patch version (bug fixes)

Final Words

If you’re serious about learning backend or full-stack development, mastering npm node package manager is non-negotiable. npm is not just a command you run in the terminal. It is the backbone of modern JavaScript development and the reason developers can build complex applications quickly without reinventing anything. 


As a beginner, your job is simple. Install packages carefully, understand what they do, integrate them properly, and manage versions responsibly. Do not blindly install npmjs packages just because a tutorial says so. 


Strong fundamentals in npm package management will save you from most beginner mistakes and give you real confidence while building projects. 

Frequently Asked Questions (FAQs)

Q1. What is npm in Node.js? 


Ans. npm stands for Node Package Manager. It is the default nodejs package manager that comes with Node.js. It allows developers to install, manage, and update NPM packages used in JavaScript and backend projects. In simple terms, npm helps you reuse code instead of writing everything from scratch. 


Q2. What is Node Package Manager used for?


Ans. The npm Node Package Manager is used to install external libraries, manage project dependencies, handle version control, run project scripts, and publish your own packages. It simplifies npm package management in both small and large apps. 


Q3. How do I install npm?


Ans. You don’t install npm separately. When you install Node.js from the official website, npm installation happens automatically. 


Q4. What are npm packages?


Ans. npm packages are reusable JavaScript modules published in the npm registry. They can be frameworks, tools, libraries, or utilities used in Node.js and frontend projects. 


Q5. What is the difference between Node.js and npm?


Ans. Node.js is a runtime environment that allows you to run JavaScript outside the browser. On the other hand, npm is the Node Package Manager that helps you install and manage libraries used in Node.js projects. You need Node.js to run apps, and you need npm to manage dependencies.

Discover More Courses on Skillwaala