• Node.js Video Tutorials

Node.js - NPM

NPM − an acronym for Node Package Manager, refers to the Command line utility to install Node.js packages, perform version management and dependency management of Node.js packages. It also provides an online repository for node.js packages/modules which are searchable on https://www.npmjs.com/. A detailed documentation of NPM commands is also available on this link.

If you are working with newer versions of Node.js (version 0.6.0 or later), the NPM utility is included with the Node.js binaries for all the OS platform bundles. Check the version of NPM in the command terminal −

PS C:\Users\mlath> npm -v

In case you have an older version of NPM, you need to update it to the latest version using the following command.

npm install npm -g

Note that YARN and PNPM tools are also available as alternatives for NPM. YARN installs packages more quickly and manage dependencies consistently across machines or in secure offline environments. PNPM (Performant NPM) is another fast and disk-space efficient package manager for Node.js packages.

If your Node.js application depends on one or more external packages, they must be installed from NPM repository. NPM packages are installed in two modes either local or global. By default, a package is installed locally.

Install Package Locally

There is a simple syntax to install any Node.js module −

npm install <Module Name>

For example, following is the command to install a famous Node.js web framework module called express −

npm install express

Now you can use this module in your js file as following −

var express = require('express');

The local mode installation of a package refers to the package installation in node_modules directory lying in the folder where Node application is present. Locally deployed packages are accessible via require() method. Use --save at the end of the install command to add dependency entry into package.json of your application.

The package.json file is a JSON file that is used to manage dependencies in Node.js projects. It contains information about the project, such as its name, version, and dependencies. The package.json file is used by the npm package manager to install and manage dependencies.

The package.json file is typically located in the root directory of a Node.js project. It can be created by running the npm init command.

Create a new folder for a new Node.js project, and run pnm init command inside it −

PS D:\nodejs\newnodeapp> npm init

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.

package name: (newnodeapp) newnodeapp
version: (1.0.0)
description: Test Node.js App
entry point: (index.js)
test command:
git repository:
keywords: test, nodejs
author: mvl
license: (ISC)

About to write to D:\nodejs\NewNodeApp\package.json −

   "name": "newnodeapp",
   "version": "1.0.0",
   "description": "Test Node.js App",
   "main": "index.js",
   "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1"
   "keywords": [
   "author": "mvl",
   "license": "ISC"

Now, if we install express package into this package locally in this project, use the following command, it also adds dependency entry into the package.json.

D:\nodejs\newnodeapp>npm install express –save

The package.json file in this folder will be updated to −

   "name": "newnodeapp",
   "version": "1.0.0",
   "description": "Test Node.js App",
   "main": "index.js",
   "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1"
   "keywords": [
   "author": "mvl",
   "license": "ISC",
   "dependencies": {
      "express": "^4.18.2"

The express package code will be placed inside the node_modules subfolder of the package folder.

If you have already placed all the project dependencies in your package.json file, all of them will be installed at once by simply running npm install (without any package name in front of it)

You can also use -save-dev flag in the npm install command to add the package as DevDepndency.

  • --save-dev installs and adds the entry to the package.json file devDependencies

  • --no-save installs but does not add the entry to the package.json file dependencies

  • --save-optional installs and adds the entry to the package.json file optionalDependencies

  • --no-optional will prevent optional dependencies from being installed

Shorthands of the flags can also be used −

  • -S: --save

  • -D: --save-dev

  • -O: --save-optional

The difference between devDependencies and dependencies is that the former contains development tools, like a testing library, while the latter is bundled with the app in production.

Install Package Globally

Globally installed packages/dependencies are stored in system directory. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but cannot be imported using require() in Node application directly. Now let's try installing the express module using global installation.

npm install express -g

This will produce a similar result but the module will be installed globally. On Linux, the global packages are placed in /usr/lib/node_modules folder, while for Windows, the path is C:\Users\your-username\AppData\Roaming\npm\node_modules.

Update Package

To update the package installed locally in your Node.js project, open the command prompt or terminal in your project project folder and write the following update command.

npm update <package name>

The following command will update the existing ExpressJS module to the latest version.

PS D:\nodejs\newnodeapp> npm update express

up to date, audited 63 packages in 2s

11 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Uninstall Packages

To uninstall a package from your project's dependencies, use the following command to remove a local package from your project.

npm uninstall <package name>

The following command will uninstall ExpressJS from the application.

PS D:\nodejs\newnodeapp> npm uninstall express
removed 62 packages, and audited 1 package in 603ms
found 0 vulnerabilities

The package entry will also be removed from the list of dependencies in the package.json file.