An Introduction To Node Package Manager (NPM)

An Introduction To Node Package Manager(NPM)

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).

If you have installed Node.js in your machine then by default NPM has already been installed. TheNode.js comes with NPM.

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

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

npm init

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

Following the Command you can install package json file:

npm install

If you want to install development dependency, then you need to follow this command line:

npm install  --save-dev

# or

npm install -D 

The difference between dependency and devDependencies are usually development tools

If you do not want to write install, then you can use i, and it will do the work for you:

npm i package-name 

If you want to save the dependency inside the package.json file , then you need to add the save flag file:

npm i package-name --save

NPM will generate one folder called node_modules:


NPM can manage packages that are local dependencies of a particular project, as well as globally-installed JavaScript tools:

Attributes of Package.json

  • name − 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 − 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 root:

sudo npm install npm -g

NPM Versioning

node package manager also manages the versioning so that you can specify any specific version of a package:

If you specify the exact version of the NPM libraries, then it also helps to 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.

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

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');
npm install express -g

Uninstalling a Module

npm uninstall <package-name>

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

npm <task-name>

json.file support specific command line.

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

Hopefully this content helps you lot. Happy coding.