NPM - Node Package Manager Overview

Discover NPM, the default package manager for Node.js. Learn how it manages dependencies, provides access to a vast repository of open-source JavaScript packages, and is a popular tool for various JavaScript frameworks. Visit the official NPM website for more details.



NPM - Node Package Manager

NPM is a command line tool for managing Node.js packages. It is also an online repository for open-source Node.js packages and is included with Node.js installation.

It has become a popular package manager for other open-source JavaScript frameworks like AngularJS, jQuery, Gulp, Bower, etc.

Official website: https://www.npmjs.com

NPM in a Nutshell

  • NPM is the default package manager for Node.js.
  • It manages dependencies for your Node.js projects.
  • It provides access to a vast repository of open-source JavaScript packages.

Key Concepts

  • Package: Reusable unit of code providing specific functionality.
  • package.json: Manages project dependencies and metadata.
  • Local vs. Global Installation: Local packages are project-specific, while global packages are accessible system-wide.

Installing Packages

  • npm install <package-name>: Installs a package locally.
  • npm install -g <package-name>: Installs a package globally (use with caution).
  • npm install <package-name>@<version>: Installs a specific version of a package.
  • npm install --save: Installs a package locally and adds it to package.json dependencies.
  • npm install --save-dev: Installs a development dependency (needed during development but not in production).

Verify NPM Installation

After installing Node.js, verify NPM installation with:

Syntax
C:\> npm -v
8.5.1

Managing Packages

  • npm update: Updates all installed packages to the latest compatible versions.
  • npm update <package-name>: Updates a specific package to the latest compatible version.
  • npm uninstall <package-name>: Uninstalls a package from the project.

Update NPM

To update NPM to the latest version, use the following command:

Syntax
C:\> npm install npm -g

Access NPM Help

To access NPM help, use:

Syntax
C:\> npm help

NPM Modes

NPM performs operations in two modes:

  • Global Mode: Affects all Node.js applications on the computer.
  • Local Mode: Affects a specific local directory.

Install Package Locally

Install a package in your local Node.js project folder with:

Syntax
C:\> npm install <package-name>

Example: Install ExpressJS locally:

Syntax
C:\MyNodeProj> npm install express

All modules installed using NPM are placed in the node_modules folder.

Add Dependency into package.json

Add a package to package.json with:

Syntax
C:\MyNodeProj> npm install express --save

Example package.json entry:

Output
{
  "name": "NodejsConsoleApp",
  "version": "0.0.0",
  "description": "NodejsConsoleApp",
  "main": "app.js",
  "author": {
    "name": "Dev",
    "email": ""
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}

Install Package Globally

Install a package globally with:

Syntax
C:\MyNodeProj> npm install -g <package-name>

Example: Install ExpressJS globally:

Syntax
C:\MyNodeProj> npm install -g express

Update Package

Update a local package with:

Syntax
C:\MyNodeProj> npm update <package-name>

Example: Update ExpressJS:

Syntax
C:\MyNodeProj> npm update express

Uninstall Packages

Remove a local package with:

Syntax
C:\> npm uninstall <package-name>

Example: Uninstall ExpressJS:

Syntax
C:\MyNodeProj> npm uninstall express

Learning Resources

Additional Tips

  • Use descriptive package names to avoid conflicts.
  • Keep dependencies up-to-date for security and bug fixes.
  • Consider using version ranges (^ or ~) in package.json to allow minor version updates without breaking compatibility.
  • Leverage tools like npm audit to identify potential security vulnerabilities in your dependencies.