Node.js NPM (Node Package Manager)

NPM (Node Package Manager) is a package manager for node.js and it was written in JavaScript to host or manage the packages of node.js. The npmjs.com contains tons of node.js packages to download and use it in our applications.

 

Generally, in node.js the package will contain all the files which are needed for a module and module is a JavaScript file that will contain a set of functions to reuse it in our applications.

 

When we install a node.js on our machine, then the NPM program will install as a command-line tool to install, update or uninstall required packages from NPM in our node.js applications.

 

To verify NPM installation, open node.js command prompt and enter the command npm -v as shown below.

 

checking npm or node package manager in node.js console

 

In case, if you want to update your older version of NPM, then you can enter the following command to update NPM to latest version.

 

npm install npm -g

Install or Download NPM Package

In node.js, we can install or download a package from npm by using following command in node.js command prompt.

 

npm install <package_name>

Here, package_name is the name of the package which you want to download. Following is the example of downloading a simple-is package from NPM using npm install simple-is command.

 

Node.js install a package from node package manager

 

After completion of package installation, we will get the result like as shown above and it will create a folder called “node_modules” and install the simple-is package in node_modules folder. In my system, the folder structure will be like C:\Users\suresh.dasari\node_modules\simple-is. In the future, if we install any other package that will be added under the node_modules folder.

 

In case, if you want to install the packages from npm in your local node.js project folder, then you can change the path to your local project folder like as shown following.

 

C:\YourProject> npm install <package_name>

List Installed NPM Packages

By using npm ls command, we can list the available NPM packages in our local machine. Following is the example getting the list of packages installed in our current working directory.

 

Node.js List Installed Packages from NPM

 

This is how we can get the list of NPM packages installed in our current directory based on our requirements.

Use Installed NPM Package

After installing the package, we can use it in our node.js applications same like other modules by including it using require directive.

 

var is = require("simple-is");

Following is the example of using installed NPM package in node.js application.

 

var http = require('http');

var is = require('simple-is');

http.createServer(function (req, res) {

    res.writeHead(200, {'Content-Type': 'text/html'});

    res.write("IsNumber: " + is.number("Welcome").toString());

    res.end();

}).listen(4200, console.info("Server listening on port 4200"));

Save the above code in a file called “simpleisexample.js” and initiate it by entering a command like as shown following in node.js command prompt.

 

Node.js initiate a server in node.js command prompt

 

Now, if anyone tries to access your computer with port 4200, then in return they will get the response in HTML format like as shown below.

 

Node.js NPM or Node Package Manager Example Result

Update NPM Package

In node.js, we can update the packages which are installed in your machine by using the following update command.

 

C:\Users\your name> npm update <package_name>

In case, if you want to update the packages which are installed your node.js project, then use the following update command.

 

C:\YourProject> npm update <package_name>

Following command will update the existing simple-is module to the latest version. 

 

C:\Users\suresh.dasari> npm update simple-is

Uninstall NPM Package

In case, if we don’t want to use the packages which are installed locally in our machine, then we can uninstall those packages by providing the module name like as shown following.

 

npm  uninstall <package_name>

Following is the example of uninstalling the package (simple-is) from local machine.

 

Uninstall a package from node.js

 

This is how we can uninstall the NPM packages by using node.js command prompt based on our requirements.