๐Ÿ Node.js Introduction & Getting Started
Estimated reading: 3 minutes 31 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

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

Or Copy Link

CONTENTS
Scroll to Top