What is Node Package Manager

Posted By : Arun Singh | 21-Jan-2018

 NPM

NPM is a package manager for Node.js Packages. When you install node.js on your system  npm  program is also installed. NPM has thousand of free packages to download and use. NPM  makes it easy for JavaScript developers to reuse other developers have shared. When someone revises the code, you can easily change your application to incorporate the newly improved code 

 

Install NPM packages

We can easily install packages of NPM. There are two ways to install NPM packages (locally or globally). 

  • Locally:- If you want to depend on the package from your own module using something like Node.js requires, then you want to install locally.
  • Globally:- If you want to use a package as command line tool then install it globally. 

A package can be installed with the command.

$ npm install <package-name>

It will create the node-modules directory in your current directory.

You can see that node-modules directory exists and that it contains a directory for the package(s) you installed. Install the package called lower-case. Confirm that it run successfully by listening to the contents of node-modules.

 

Install Packages Locally

 $npm install lower-case
 $ls node_modules
$lower-case

Example:

 

The installed lower-case package is ready to use.

var lowerCase = require('lower-case');
var http = require('http');
  http.createServer(function(req,res){
       res.writeHead(200, {'Content-Type': 'text/html'});
       res.write(lowercase("HELLO!, HOW ARE YOU"));
       res.end();
}).listen(8080);

Output:

hello! how are you

If you install packages locally, you normally do so using a package.json file. 

 $npm init
 package-name:  (project)
 version:   (1.0.0)
 Description:  Demo package.json
 entry point:  (index.js)
 test command:
 git repository:
 keywords:
 author:
 license: (ISC) 

Click on enter to accept the defaults, then type 'yes' to confirm. It will create package.json file at the root of the project.

{
"name" : "project",
"version" : "1.0.0",
"description" : "",
"main" : "index.js",
"scripts": {
  "test" : "echo \"Error: no test specified\" && exit 1"
 },
"author" : "",
"license" : "ISC"
}

Installing Package Globally

At the moment we have one package installed globally -- that is npm package itself.

$npm install uglify-js --global

Listing Global Packages

$npm list --global

Output:

|--|-- [email protected]
|  |---- [email protected]
|  |---- [email protected]
|  |---- [email protected]
..........................
|---- [email protected]
   |---- [email protected]
   |  |---- [email protected]
   |---- [email protected]                                                                                                       

 

 

 

 

 

 

 

 

  

About Author

Author Image
Arun Singh

Arun is a MEAN stack developer. He has a fastest and efficient way of problem solving techniques. He is very good in JavaScript and also have a little bit knowledge of Java and Python.

Request for Proposal

Name is required

Comment is required

Sending message..