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
$npm install lower-case
$ls node_modules
$lower-case
Example:
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
$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
{
"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 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]
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.