Node.js NPM - Node.js

What is Node.js NPM?

Node Package Manager (NPM) provides two main functionalities −

  • Online repositories for node.js packages/modules which are searchable on search.nodejs.org
  • Command line utility to install Node.js packages, do version management and dependency management of Node.js packages.

NPM comes bundled with Node.js installable after v0.6.3 version. To verify the same, open console and type the following command and see the result –

If you are running an old version of NPM then it is quite easy to update it to the latest version. Just use the following command from root –

Installing Modules using NPM

There is a simple syntax to install any Node.js module −

For example, following is the command to install a famous Node.js web framework module called express −

Now you can use this module in your js file as following −

Global vs. local installation

by default, NPM installs any dependency in the nearby mode. here nearby mode refers to the package installation in node_modules directory lying within the folder wherein Node software is present. locally deployed packages are accessible via require() method. as an instance, while we hooked up the explicit module, it created a node_modules directory in the current directory where it installed the express module.

Alternatively, you can use npm ls command to list down all the locally installed modules.

Globally installed packages/dependencies are stored in system listing. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but cannot be imported using require () in Node application directly. Now let's try putting in the explicit module using global installation.

This will produce a similar result but the module will be installed globally. Here, the first line shows the module version and the location where it is getting installed.

You can use the following command to check all the modules installed globally −

Using package.json

The package.json is present in the root directory of any Node application/module and is used to outline the residences of a package. Let’s open package.json of specific package found in node_modules/express/

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

Uninstalling a Module

Use the following command to uninstall a Node.js module.


Once NPM uninstalls the package, you can verify it by looking at the content of /node_modules/ directory or type the following command −

Updating a Module

Update package.json and change the version of the dependency to be updated and run the following command.

Search a Module

Search a package name using NPM.

Create a Module

Creating a module requires package.json to be generated. Let's generate package.json using NPM, which will generate the basic skeleton of the package.json.

You will need to provide all the required information about your module. You can take help from the above-mentioned package.json file to understand the meanings of various information demanded. Once package.json is generated, use the following command to register yourself with NPM repository site using a valid email address.

It is time now to publish your module −

If everything is fine with your module, then it will be published in the repository and will be accessible to install using NPM like any other Node.js module.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Node.js Topics