๐Ÿ Node.js Introduction & Getting Started
Estimated reading: 3 minutes 280 views

Node.js โ€“ NPM (Package Manager) โ€“ Complete Beginnerโ€™s Guide to Node Package Manager


Introduction โ€“ What Is NPM in Node.js?

NPM (Node Package Manager) is the default package manager for Node.js and the largest software registry in the world. It allows developers to install, share, and manage open-source packages, dependencies, and scripts in Node.js applications.

NPM not only simplifies code reuse but also makes it easy to manage project dependencies, automate scripts, and publish your own libraries.

In this guide, youโ€™ll learn:

  • What NPM is and why itโ€™s essential for Node.js
  • How to install and use packages
  • Difference between local vs global packages
  • How to manage dependencies and create package.json
  • Useful NPM commands for real projects

What Is NPM?

NPM stands for Node Package Manager. It comes bundled with Node.js and offers:

Feature Description
Dependency MgmtAutomatically installs libraries listed in package.json
Script RunnerLets you define and run custom project tasks
CLI ToolSimple terminal interface to install/update packages
Publishing ToolHelps developers publish and distribute Node.js libraries

Check if NPM is installed:

npm -v

What Is package.json?

The package.json file is the heart of every Node.js project. It contains metadata and lists all the project dependencies.

Create one with:

npm init

Or quickly with default values:

npm init -y

Sample package.json:

{
  "name": "my-node-app",
  "version": "1.0.0",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "^4.18.2"
  }
}

Installing Packages

Install a Single Package Locally:

npm install lodash

This installs the package into node_modules/ and adds it to package.json under dependencies.

Install Multiple Packages:

npm install axios dotenv express

Global vs Local Packages

Type Scope Example
LocalInstalled in project (node_modules/)npm install express
GlobalAvailable system-wide via CLInpm install -g nodemon

Use global packages for tools and CLIs like:

npm install -g nodemon

Updating & Uninstalling Packages

Update a Package:

npm update lodash

Uninstall a Package:

npm uninstall lodash

Viewing Installed Packages

List local packages:

npm list --depth=0

List global packages:

npm list -g --depth=0

Useful NPM Commands

Command Description
npm installInstalls all packages from package.json
npm startRuns the script defined as start
npm run <script>Runs a custom script defined in package.json
npm outdatedLists outdated packages
npm auditScans for vulnerabilities
npm ciClean install from lock file (for CI/CD)

Example โ€“ Use NPM with Express

npm init -y
npm install express

Then create app.js:

const express = require('express');
const app = express();

app.get('/', (req, res) => res.send('Hello from Express!'));

app.listen(3000, () => console.log('Server running on port 3000'));

Run:

node app.js

Output:

Server running on port 3000

Summary โ€“ Recap & Next Steps

NPM makes managing Node.js projects easy and powerful. It simplifies installing libraries, running scripts, and organizing your development workflow with package.json.

Key Takeaways:

  • NPM is bundled with Node.js and manages project dependencies
  • Use npm install, uninstall, and update for package control
  • Scripts in package.json streamline your development tasks

Real-world relevance:
Every modern Node.js projectโ€”from REST APIs to React appsโ€”relies on NPM for scalability and modularity.


FAQs โ€“ Node.js NPM (Package Manager)


What is the difference between npm and npx?
npm installs packages, while npx runs a package without installing it permanently.


How do I install all dependencies from a cloned project?
Run:

npm install

Can I install a specific version of a package?
Yes:

npm install lodash@4.17.21

What is package-lock.json?
It locks the exact versions of installed packages for consistent installs across environments.


Is NPM only for backend development?
No. NPM is used for both backend (Node.js) and frontend (React, Vue, Angular) development.


Share Now :
Share

๐Ÿ“ฆ Node.js โ€“ NPM (Package Manager)

Or Copy Link

CONTENTS
Scroll to Top