Node Package Manager (NPM) is a command line tool that installs updates or uninstalls Node.js packages in the application. It is an online repository for open-source Node.js packages. The node community around the world creates useful modules and publishes them as packages in this repository.
NPM provides two main functionalities:
1. It provides online repositories for node.js packages/modules which are searchable on search.nodejs.org.
2. It also provides command line utility to install Node.js packages, do version management and dependency management of Node.js packages.
NPM is included with Node.js installation. After installing Node.js, we can verify NPM installation by using the following command on the terminal or command prompt.
If we have an older version of NPM then it can be updated it to the latest version using "npm install npm –g" command.
To access NPM help, write "npm help" in the command prompt or terminal window.
NPM performs the operation in two modes:
1. Global and
In the global mode, NPM performs operations which affect all the Node.js applications on the computer whereas, in the local mode, NPM performs operations for the particular local directory which affects an application in that directory only.
Install the package Locally:
Below given command can be used to install any third party module in the local Node.js project folder.
For example, the following command will install ExpressJS into node folder of D drive.
All the modules installed using NPM are installed in node_modules folder.
Add Dependency into package.json:
Command "npm install <module_name> –save" can be used to add dependency entry into package.json of the application.
For example, "npm install express –save" will install ExpressJS in the application and adds dependency entry into the package.json as well.
The package.json of the project will look something like below.
Install Package Globally:
NPM can also install packages globally so that all the node.js application on that machine can import and use the installed packages.
NPM installs global packages into "/<User>/local/lib/node_modules" folder. Apply -g in the install command to install the package globally.
Command to install ExpressJS globally is given below:
To update the package installed locally in the Node.js project, we can run the following command on command prompt or terminal window on the project folder.
Updating the ExpressJS using the following command:
Searching a Module:
"npm search express" command is used to search express or module.D:\node>npm search express
Use the following command to remove a local package from the project.
The following command will uninstall ExpressJS from the application.