NPM- Node Package Manager

NPM is the default dependency management tool for the NodeJS projects. It uses ‘package.json’ for configuring project details. The configurations include dependencies, scripts tasks, repository location and many other stuffs.

As a dependency management tool, it not only help us download and include direct dependencies but, also takes care of the dependencies of these direct dependencies internally.

We will focus on different types of dependencies and their npm commands in this article. And, discuss on the other configurations supported by npm through package.json in a separate article.

Note :

1. If we are familiar with maven, NPM is a similar tool for NodeJS as maven is for Java projects. And, the package.json in npm is similar to pom file in maven.

2. YARN is an alternative to npm. And, both of these tools use package.json for project configuration.

Including Core Libraries Vs External Libraries

  • As shown in the diagram, NodeJS provides the runtime environment and the core libraries. 
  • The NPM registry has a huge collection of useful NodeJS libraries. Using NPM we can easily download the specified versions of these external libraries and make it part of our projects. 
  • After installing an external library using npm, we can include them in our projects in a similar way as we include the core libraries using require
//The core libraries are part of NodeJS installation
//Including a core library- fs in our program. 
let fs = require('fs'); 

//Including an external library - express in our program 
let app =require('express'); 

Installing All External Dependencies using NPM

We use three major types of dependencies in NodeJS projects: 
1. dependencies – Includes libraries that needs to be bundled with the app in production. 
2. devDependencies – Includes build-libraries, testing-libraries required for development and quality checks. We do not need these in our production builds.
3. optionalDependencies – Its a less commonly used field but, as the name suggest it includes optional dependencies. In other words, it says the application has been built to work even if these dependencies are missing.

We can find these categories in ‘package.json’ file. And, to install or download all these listed dependencies for a project we can use the following command :

npm install  

NPM also allows us to add or remove individual dependencies as described below.

Adding Individual Dependencies

1. Local installation : Adding a library for a specific project

npm install <package-name>

//To add 'express' library into our project
npm install express 

Using the above command, we can add an external package under ‘dependecies’ list in ‘package.json’. This downloads the package into node-modules subfolder of the current project.  

The ‘install’ command comes with following key options for specifying type of the dependency :

  1. –save-dev : It adds the package under list of ‘devDependencies’ in package.json.
  2. –no-save : It does not add the downloaded package as a project dependency in package.json.
  3. –save-prod or –save : It’s the default option and we need not specify it explicitly. And, it adds the installed package under ‘dependencies’ list.


2. Global Installation – Adding a library to share across projects

npm install -g <package-name>

Using ‘-g’ option as shown above, we can install external libraries into global locations. This is useful for libraries shared across multiple projects. Generic development tools like ‘create-react-app’, ‘http-server’ etc are good candidates for global installations.

The -g option installs the library into the global location. The global location can be found using :

F:\nodejs\node01\first-project>npm root -g


Removing Individual Dependencies

The command to remove a package from node-modules directory.

//For removing from local node-modules directory
npm uninstall <package-name>

//For removing from global node-modules directory
npm uninstall -g <package-name>

The command to remove a package from local node-modules and dependencies list in package.json :

//Command to remove from dependencies list 
npm uninstall -save <package-name>

//Command to remove from devDependencies list 
npm uninstall -save-dev <package-name>



Apart from dependency management, npm along with package.json help us manage various aspects of project development. It helps us in managing various command line scripts, publishing the package, and maintain many other project related details. We will discuss more on this as part of the article on package.json