Node.js – Node Package Manager

NPM is an abbreviation for Node Package Manager. It is the default package manager developed by Isaac Z. Schlueter using JavaScript Programming Language, designed for JavaScript Runtime Environment that is Node.js. with a primarily release on January 12, 2010.

Node Package Manager (npm) is a command-line utility that allows the user to install, update, or uninstall the Node.js packages in the application. Moreover, NPM provides online user repositories for Node.js packages and modules. A surplus of these libraries and applications is being published every day on npm and accessed through the portal:

Node.js NPM

Node Packaged Modules (npm) are installed at the installation time of Node.js. We can check the version by typing the following command on any Node.js Command-line shell or terminal:

Node.js NPM

Understanding package.json

As per the guidelines, if any project that involves the use of Node.js, it is necessary to have a file named package.json. So, what precisely a package.json file is?

Node.js NPM
Node.js NPM

Let us understand it in brief, a package.json file is a file that can be defined as a manifest of our Project, including the packages and the applications it relies on, its unique source control information, and metadata related to the Project.

Within a package.json, we often discover metadata related to the Project. This metadata helps us identify the Project and works as a reference point for both the users and the contributors to get info regarding the Project.

Here’s an example showing the fields in a package.json file:

File: package.json

A package.json file is defined in the JSON format (stands for, JavaScript Object Notation format), which helps the machine to easily read and analyze its metadata.

Node.js NPM

However, to make changes in the package.json file or to create a new package.json file for another project, we can execute the following command in Node.js command line to initialize the framework for the Project:

The npm init command initializes a procedural tool to frame out the Project. This procedure includes all details as user input, as follows:

  • The Name of the Project
  • The Initial Version of the Project
  • The Description of the Project
  • The Main File for the Project
  • The Test Command for the Project
  • The Git Repository for the Project
  • The Tags related to the Project (basically, the keywords)
  • The License for the Project (By default, it is set to ISC, an open-source software license which has been written by Internet Systems Consortium and functions equivalent to MIT License)
Node.js NPM

We can also use the –yes flag to instantly initialize the Project. This flag can be used with the central command to populate all the options with default entries automatically. The use of the flag can be seen as follows:

Understanding the Dependencies of Project

Apart from the Project’s information, there is a significant aspect to understand in package.json, which holds a collection of dependencies related to the Project. These dependencies make the functioning of the Project properly as these are the modules to that Project.

File: package.json

As it is visible, these dependencies allow the Project to install the modules, it depends on. It can be installed by typing the following command on Node.js command line or terminal:

Here, as we can see, the <module-name> is the name of the module, which we are installing in our Project. For Instance, here we’ve installed two modules – Express (the most used and well-known framework for Node.js) and Validator, for our Project. So, we can type:

Node.js NPM

The following command will install the express module into /node_modules folder, in the current directory and update the package.json, respectively. Moreover, we can add an optional flag –save or –save-dev (for developer dependency) to the command, to install and save the modules in package.json dependencies.

Apart from this, there is the –global or -g flag used to install modules globally on our system, rather than stored locally.

Using package in Node.js project

After installing the package (e.g., Validator), it is ready to use.

We can use it in the Project by including the ‘validator’ package, the same way we include the other modules.

Let us understand the use of the ‘validator’ package through an example.

File: app.js

In the above snippet, we are defining a string and passing a validator to check if the input string is an email or not. For the execution of this function, we are using validator.isEmail(), to check if the input email-address contains all the valid attributes (e.g., [email protected]) or not. The function will give an output in terms of true or false. Moreover, we’ve used validator.isURL() to check if the input string is a URL.

The output of the above example: app.js is shown below:

Node.js NPM

Similar to the above example, there are lots of different validators available at

Understanding npm ls

We’ve already installed few packages to our Project. But wonder what modules do these packages hold? Since we’ve studied that the package.json file for a project contains all the information that helps to structure a project; and have discussed dependencies in previous sections. We also discovered that once a package is installed, /node_modules folder gets populated. However, at a certain level, while exploring these files and folders, it becomes a bit difficult to understand what we’ve got in our package, and what we can do to rectify.

Therefore, let us explore the npm ls API, in brief. The npm ls is a command used on the command-line shell to list down all the dependencies that are installed to node_modules folder. Moreover, it will return a non-zero exit code, if the dependencies differ from what in node_modules contains and what package.json has.

We can type the following command:

This command will populate the screen with the list of all the modules installed with the package. The output of the same is shown below:

While scrolling down, a bunch of lines with deduped at the end can be visible. The deduped indicates that npm was ready to resolve a version of that module that satisfied the requisites of various dependencies that need to be installed.

Some Essential Commands in npm

As we’ve seen, the “Node Package Manager” helps to manage modules that are required in the Node.js applications.

Let us discover some more functions related to npm that are necessary for managing modules:

  1. Uninstalling a Package: The way we install a Package, the same way we can uninstall one. The command that we use to uninstall a package is as follows:
  • Searching for a Package: We can use the following command to search whether a specific package and its version is available locally or not:
  • Updating a Package: As the new version releases, the package requires an update, and for that, we can use the following command:
  • npm help: npm help command navigates the user to help section of npm. We can use the following command:
Node.js NPM

Pin It on Pinterest

Share This