Learn How to Install Modules Through The NodeJS Package Manager

1
1293
nodejs-package-manager

In this article, we are going to discuss on NodeJS Package Manager. how to install existing Node.js modules, updating, and searching of these existing Node.js modules through the Package Manager (NPM). Later in this article, we will learn about the local and global installation of packages, use of package.json file, and other useful commands to create a new Node.js module, creation of a new user to the NPM repository and publishing the new Node.js module to the repository.

Functionalities of Node Package Manager (NPM)
It has the following two core functionalities.

  • It provides online repositories for node.js packages or modules. These modules or packages can be searched through search.nodejs.org.
  • It provides the command line utility through which we can install Node.js packages, maintain version management and dependency management of Node.js packages.

NPM Version
We can check the version of NPM after executing the following command as shown below.

version-of-npm
When we install Node.js on our machine which is version v0.6.3 and above, the NPM will be integrated with it. Therefore, if your machine has an older version of Node.js then it can be updated to the required version (here 2.15.8) after executing the following command.

install-node-js

How to Installing Modules using NPM
Installing new Node.js modules using NPM is very simple and can be done by executing the following command.

For example, if want to install the popular ‘express’ Node.js web framework then it can be easily done after executing the following command.

install-express
When we want to use this Node.js module for our development, then we just need to import this module by using the following code.

Local vs Global Installation
Node.js modules can be installed in two ways i.e. local and global installation. The following is the comparison between both.

Local installation refers to the package on the local machine where installation takes place in the node_modules directory present inside the Node application. If packages are installed locally then these packages can be accessed through ‘require ()’ method as shown above.

Globally installed packages on the other hand have their dependencies stored in the system directory. We can use these dependencies in the CLI (Command Line Interface) function of any node.js. However, we cannot import these by using ‘require ()’ method in Node application directly.

We can install the express module as global installation by executing the following command.

After global installation, we can observe the module version (4.14.0) and the actual installation location/path (C:\Users\Aparajita\AppData\Roaming\npm) as shown below.

Command to check all globally installed modules

Use of package.json
The package.json is the JSON formatted file which is present at the root directory of any Node.js application or module. It is basically used to define the properties of a package. If we open the package.json file of the installed express package present at path ‘C:\Users\Aparajita\AppData\Roaming\npm\node_modules\express’ It will like this:

The following are the attributes of the package.json file: –

S No

Attribute

Description

1.

name

It denotes the name of the package.

2.

version

It denotes the version of the package.

3.

description

It denotes the description of the package.

4.

homepage

It denotes the homepage of the package.

5.

author

It denotes the author of the package.

6.

contributors

It denotes the name of the contributors to the package.

7.

dependencies

It denotes the list of dependencies present in the package. All the dependencies are automatically installed by the NPM inside the node_module folder of the package.

8.

repository

It denotes the repository type and the URL of the package.

9.

main

It denotes the main entry point of the package.

10.

keywords

It denotes various keywords present in the package.

11.

maintainers

It denotes the name of the maintainers of the package.

Other useful Commands for NPM
1. Uninstalling a Node.js Module: – We can uninstall a Node.js module after executing the following command on the command line.

2. Updating a Node.js Module: – We can update a Node.js module after executing the following command on the command line.

3. Searching a Node.js Module: – We can search a Node.js module after executing the following command on the command line.

4. Creating a Node.js Module: – We can create a Node.js module after executing the following command on the command line.

5. Registration a user to NPM Repository: – We can use the following command to register ourselves with NPM repository site. The site will ask to enter a username, password and email id. Once user is created, it will be logged on to the repository (https://registry.npmjs.org/).

6. Publishing a Node.js Module to Repository: – We can publish a Node.js module after executing the following command on the command line.

After executing the above command, we can published our Node.js module to the repository which can be accessed to install using NPM like any other Node.js module.

Learn Nodejs by building 12 Projects From Scratch at Eduonix Courses

Source Code for Package Manager in NodeJS

Conclusion: –
In this chapter, we have covered all the basic aspects of Node package manager (NPM) required for a beginner developer to work on it.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here