What is NPM(Node package manager): Complete Guide

NPM is the biggest repository for any programming language and has almost every package you need in a web or mobile development project. The npm means the node package manager

The topics we cover are the following.

NPM Topics

  1. Overview of NPM
  2. Attributes of package.json
  3. Updating NPM packages
  4. NPM Versioning
  5. Semantic versioning
  6. Global and local installation of NPM packages.
  7. Uninstalling module.
  8. Search a module
  9. Running an NPM task

What is Node Package Manager(NPM)

Node Package Manager is only for Node.js development and nothing else. Still, as the JavaScript community grows, it becomes the backbone of almost all the latest JavaScript Frameworks and Web development. In addition, the npm registry hosts the world’s most extensive free, reusable code collection.

NPM is written entirely in JavaScript(JS) and was developed by Isaac Z. Schlueter with inspiration from the shortcomings of other similar projects such as PEAR (PHP) and CPAN (Perl).

NPM package manager is familiar to you if you have previous work with any front-end development stuff. 

Over 350,000 packages were reported to be listed in the npm registry, making it the most significant single language code repository on the planet.  You can go to its original website: https://www.npmjs.com/. The Node Package Manager or NPM manages downloads of dependencies of your project.

If you have installed Node.js on your machine, then NPM has already been installed by default. This is because Node.js comes with NPM.

You can check the version of your NPM using the following command.

An Introduction To Node Package Manager(NPM)

Your dependencies are written in one file inside your project root called package.json.

You can create the package.json file using the following command.

npm init

If you already have the package.json file, but dependencies are not installed previously, you can install all the dependencies defined on your package.json file using the following command.

npm install

If you want to install development dependency, you can hit the following command with the  –dev flag.

npm install <package-name> --save-dev

# or

npm install -D <package-name>

The difference between dependency and devDependencies is usually development tools, like a testing library, while dependencies are bundled with the app in production.

If you do not want to write install, you can use i, which will do the work for you.

npm i <package-name>

To save the dependency inside the package.json file, you must add the –save flag.

npm i <package-name> --save

NPM will generate one folder called node_modules when you install any library from the Node Package Manager Repository.


NPM can manage packages that are local dependencies of a particular project and globally-installed JavaScript tools. We can use it as a dependency manager for our local project.

Attributes of Package.json

  • name − the name of the package
  • version − version of the package
  • description − description of the package
  • homepage − homepage of the package
  • author − author of the package
  • contributors − the name of the contributors to the package
  • dependencies − list of dependencies. NPM automatically installs all the dependencies mentioned here in the node_module folder of the package.
  • repository − repository type and URL of the package
  • main − entry point of the package
  • keywords − keywords

Updating NPM Packages.

You can update NPM packages using the following command.

npm update

You can also update the specific package using the following command.

npm update <package-name>

If you are running an old version of NPM, then you can update it to the latest release by the following command from the root.

sudo npm install npm -g

NPM Versioning

In addition to plain downloads, the node package manager manages the versioning so that you can specify any specific package version.

If you specify the exact version of the NPM libraries, then it also helps keep everyone on the same version of a library so that the whole team runs the same version and no conflicts occur until the package.json file is updated.

Using the Git version control system, you must upload the package.json file, notde_modules folder.

So when another developer downloads the project, it already has the package.json file, and he only needs to hit the npm install command to get up and running with the project.

If we do not specify any version, it will install the latest version of the package.

Semantic Versioning

  • To install a package of a specific version, mention the full and exact version in the package.json file.
  • To install the latest version of the package, mention “*” in front of the dependency or “latest”. This will find the latest stable version of the module and install it.
  • To install any version (stable one) above a given version, mention it like in the example below:
    “express”:” ^4.1.1″. in the package.json file. The caret symbol (^) is used to tell the npm to find a version greater than 4.1.1 and install it.

Global vs. Local Installation

By default, NPM installs any dependency in the local mode. Here local mode refers to the package installation in the node_modules directory inside our working project.

Locally deployed packages are accessible via the require() method.

For example, when we installed an express module, it created a node_modules directory in the current directory where it installed the express module.

$ npm install express

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

var express = require('express');

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

npm install express -g

It will produce a similar result, but the module will be installed globally.

Uninstalling a Module

Use the following command to uninstall a Node.js module.

npm uninstall <package-name>

Search a Module

Search a package name using NPM.

npm search express

Running Tasks

The package.json file supports a format for specifying command-line tasks that can be run using the following command.

npm <task-name>

For example:

  "scripts": {
    "start-dev": "node lib/server-development",
    "start": "node lib/server-production"

It’s very common to use this feature to run Webpack:

  "scripts": {
    "watch": "webpack --watch --progress --colors --config webpack.conf.js",
    "dev": "webpack --progress --colors --config webpack.conf.js",
    "prod": "NODE_ENV=production webpack -p --config webpack.conf.js",

So instead of typing those extended commands, which are easy to forget or mistype, you can run:

$ npm watch
$ npm dev
$ npm prod

That’s it for this tutorial.

Recommended Posts

What is process.env in Node.js

How to build a Node web server

Node Streams

AWS Lambda And Node.js

ES6 Modules in Node

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.