Install Packages Using npm install

Learn via video courses

Overview

While working on a project we often need to import certain modules or packages so as to use them inside our project. With the utilization of NPM (Node Package Manager) one can easily install the required node package onto their local system and access the requisite functions and modules present inside that package. So, Let's dive into the article to learn how to install packages with NPM.

Introduction to NPM

NPM or the Node Package Manager is a command line utility that enables us to install various packages under Node.js.The packages inside npm are all available as open-source files. Npm manages all the packages for Node.js. To use npm one needs to have Node.js installed in their local system.

With the use of npm, one can install, update and uninstall packages. Also npm enables users to handle dependencies when working on a project.

For installation of any specific package the given command is used :

Example:

Explanation: In the above Example npm install express loads the express package (framework for Node.js) into the local directory where the command line is running.

Similarly, to uninstall a package npm uninstall <package_name> command is used.

To install a package with a specific version :

One can also ensure that a package has been installed inside a specified directory by using the command

If the output of the above command was <packagename>@version then the installation was successful. Otherwise if empty was returned then the package was not installed properly.

While working on any node project there are two important json files i.e. package.json and package-lock.json that contain metadata(version, author, etc. ) about the project.

Whenever one installs a node package then the name of the package with its build version gets appended to the dependencies.

Package.json file looks something like this:

In the above case after executing the command npm install nodemon nodemon and it's current version are appended to the dependencies section.

Every package in the node gets installed as a dependency. Npm also allows a developer to install a package as development dependency only.

Local Install

If we want to install any node package to a specific directory then we need to use the given command

Example:

In the above example npm install express loads the modules and dependencies inside the express package locally at the path specified by C:\WebApp.

Global Install

When we need to install some package in a way such that we can use modules and functions inside that package from any directory then global installation should be used. Globally installing a package eliminates the need to install each package repetitively for every new project created.

The command for global installation is :

Example:

In the above example npm install -g mongoose loads the modules and dependencies inside the mongoose package globally which means modules of this package are accessible at every location in the system.

Installing a Package with dist-tag

Dist-tags or Distribution-Tags are human-readable labels that are used for labelling different versions of a package. In simple words, Tags can be used to provide an alias instead of version numbers. For example, npm currently uses the tag "latest" to identify the current version and the tag "next" to identify the upcoming version. Similarly, we can give our own dist-tags to the package version while publishing any package. So if a package is published with a dist-tag, then we can install it using the dist-tag instead of specifying the version of the package.

Syntax:

You can also list all the distribution tags or dist-tags of any package by:

Example:

Conclusion

  • Npm is a free open-source package manager that is used for installing, maintaining and sharing node packages and dependencies in a project.
  • Installation,updation and removal of packages can be easily done through command-line locally into a directory or globally for the entire system.
  • package.json and package-lock.json are two files in a project which contain metadata(version, author etc. ) about the project and the packages installed.
  • For installing a specific version of the package, we can install it by adding the version or dist-tag (distribution-tag) of that package.