Node Package Manager or NPM is a command line tool used while working with Node.js. NPM provides the following features for Node.js developers:
- As there are lots of packages for Node.js ,NPM makes it easier to manage them.It represents a repository of all the packages available for Node.js. We can install the packages using NPM.
- As all the packages are present in central Node.js repository we can search these using NPM.We can use NPM to search a particular package as :
npm search "package name"
NPM important commands
Following are useful commands for managing node.js packages
To download a package
npm install Package_Name_To_Install
For example following will download a package named test_package
npm install test_package
This command will download the mentioned package to the node_modules directory.It will create node_modules directory
in the current directory if it doesn’t exist.
Using a package
We can use a package which is in the node_modules directory in our application by using the require() method.
For example we can use a module named test_package in our application as:
var lodash = require('lodash');
To install a package globally we use the following command:
npm install package_name -g
global packages differs from local packages in following ways:
- They are installed in the system directory
- They can be used from the command line
- They can not be used in a package using require
View the installed packages
To view the list of all locally installed modules using the following command
Above command will display the list of packages as well as the location where the packages are installed