Overview

NPM (Node Package Manager) is the default package manager for Node.js. It is a important tool for JavaScript developers, providing access to a large repository of packages and a convenient way to manage dependencies in Node.js projects.

NPM simplifies tasks such as package installation, version management, and script execution.

Installation

Install using Node Version Manager

(This allows you to install multiple versions of Node)

Install using Homebrew

(Only for MacOS & Linux)


Download the installer

Choose your preferred Node version, Operating System and click the download button

Installation

Once the installer is downloaded, just install it like you install any other software

Usage

Initializing a Project

npm init

This command creates a package.json file in your project directory, which is used to manage the project's dependencies, scripts, and other metadata.


Installing a Package

npm install <package_name>

This command installs a package locally in your project. It also updates the package.json and package-lock.json files to reflect the new dependency.

Example

npm install express

This installs the express package, a popular web framework for Node.js.


Installing a Package Globally

npm install -g <package_name>

This command installs a package globally on your system, making it available from any directory.

Example

npm install -g nodemon

This installs nodemon, a tool that automatically restarts Node.js applications when file changes are detected, globally.


Updating a Package

npm update <package_name>

This command updates a package to the latest version. If no package name is specified, it updates all project dependencies.

Example

npm update express

This updates the express package to its latest version.


Uninstalling a Package

npm uninstall <package_name>

This command removes a package from your project and updates the package.json and package-lock.json files accordingly.

Example

npm uninstall express

This uninstalls the express package.