In the last article we studied how to use REPL Terminal to execute basic node.js code through command window. In this article we are going to study node.js NPM which is another very handy tool. Node.js NPM stands for node.js package manager and it has two primary tasks. 1) It is used to download node.js packages available at search.nodejs.org. 2) It provides a command line interface to install these modules, maintain versioning and dependency issues. It has almost same functionalities as nuget package manager has in visual studio.
NPM comes bundled with node.js by default. To check the current version of NPM installation, simply type “npm –version” on the command prompt.
Installing a Package via Node.js NPM
It is very easy to install any package via npm. You simply have to write “npm install ” in the command window. For instance, to install “express” module which is one of the most famous node.js web frameworks, simply type the following command “npm install express”. This will install the express module inside a directory named “node_modules” which is inside the directory where you currently running the node.js command window. This is local installation. You can get all the local modules via command “npm ls”. Now you can access local modules via var exp = require(‘express’) command.
The packages installed via “npm install ” are installed locally. Which means that they are installed in the directory where the node.js application is currently running. You can also install node.js modules globally via “npm install -g”. This will install the package in the global system directory.
Uninstalling a Package
Uninstalling a node.js package is extremely simple. You have to use “npm uninstall ” command. You can verify if the package has actually been uninstalled by checking the directory where it was installed or by listed the packages via “npm ls” command.
Updating a Package
To update a package, simply type “npm update .”
Search a Package
Similarly, you can also search installed packages. To do so, type “npm search . This will return the package if exists.”