npm, short for Node Package Manager, is two things: first and foremost, it is an online repository for the publishing of open-source Node.js projects; second, it is a command-line utility for interacting with said repository that aids in package installation, version management, and dependency management. A plethora of node.js libraries and applications are published on npm, and many more are added every day. These applications can be searched for on http://search.npmjs.org/. Once you have a package you want to install, it can be installed with a single command-line command.
Let’s say you’re hard at work one day, developing the Next Great Application. You come across a problem, and you decide that it’s time to use that cool library you keep hearing about – let’s use Caolan McMahon’s async as an example. Thankfully, npm is very simple to use: you only have to run npm install async, and the specified module will be installed in the current directory under ./node_modules/. Once installed to your node_modules folder, you’ll be able to use require() on them just like they were built-ins.
Let’s look at an example of a global install – let’s say coffee-script. The npm command is simple: npm install coffee-script -g. This will typically install the program and put a symlink to it in /usr/local/bin/. This will then allow you to run the program from the console just like any other CLI tool. In this case, running coffee will now allow you to use the coffee-script REPL.
Another important use for npm is dependency management. When you have a node project with a package.json file, you can run npm install from the project root and npm will install all the dependencies listed in the package.json. This makes installing a Node project from a git repo much easier! For example, vows, one of Node’s testing frameworks, can be installed from git, and its single dependency, eyes, can be automatically handled:
Example:
git clone https://github.com/cloudhead/vows.git
cd vows
npm install
After running those commands, you will see a node_modules folder containing all of the project dependencies specified in the package.json.
NPM is included with Node.js installation. After you install Node.js, verify NPM installation by writing the following command in terminal or command prompt.
C:\> npm -v
2.11.3
If you have an older version of NPM then you can update it to the latest version using the following command.
C:\> npm install npm -g
To access NPM help, write npm help in the command prompt or terminal window.
C:\> npm help
NPM performs the operation in two modes: global and local. 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.
Global vs Local Installation
By default, NPM installs any dependency in the local mode. Here local mode refers to the package installation in node_modules directory lying in the folder where Node application is present. Locally deployed packages are accessible via require() method. For example, when we installed express module, it created node_modules directory in the current directory where it installed the express module.
$ ls -l
total 0
drwxr-xr-x 3 root root 20 Mar 17 02:23 node_modules
Alternatively, you can use npm ls command to list down all the locally installed modules.
Globally installed packages/dependencies are stored in system directory. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but cannot be imported using require() in Node application directly. Now let’s try installing the express module using global installation.
$ npm install express -g
This will produce a similar result but the module will be installed globally. Here, the first line shows the module version and the location where it is getting installed.
[email protected] /usr/lib/node_modules/express
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected])
You can use the following command to check all the modules installed globally −
$ npm ls -g
Install Package Locally
Use the following command to install any third party module in your local Node.js project folder.
C:\>npm install <package name>
For example, the following command will install ExpressJS into MyNodeProj folder.
C:\MyNodeProj> npm install express
All the modules installed using NPM are installed under node_modules folder. The above command will create ExpressJS folder under node_modules folder in the root folder of your project and install Express.js there.
Add Dependency into package.json
Use –save at the end of the install command to add dependency entry into package.json of your application.
For example, the following command will install ExpressJS in your application and also adds dependency entry into the package.json.
C:\MyNodeProj> npm install express –save
The package.json of NodejsConsoleApp project will look something like below.
package.json
{
“name”: “NodejsConsoleApp”,
“version”: “0.0.0”,
“description”: “NodejsConsoleApp”,
“main”: “app.js”,
“author”: {
“name”: “Dev”,
“email”: “”
},
“dependencies”: {
“express”: “^4.13.3”
}
}
Install Package Globally
NPM can also install packages globally so that all the node.js application on that computer 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 package globally. For example, the following command will install ExpressJS globally.
C:\MyNodeProj> npm install -g express
Update Package
To update the package installed locally in your Node.js project, navigate the command prompt or terminal window path to the project folder and write the following update command.
C:\MyNodeProj> npm update <package name>
The following command will update the existing ExpressJS module to the latest version.
C:\MyNodeProj> npm update express
Uninstall Packages
Use the following command to remove a local package from your project.
C:\>npm uninstall <package name>
The following command will uninstall ExpressJS from the application.
C:\MyNodeProj> npm uninstall express
Uninstalling a Module
Use the following command to uninstall a Node.js module.
$ npm uninstall express
Once NPM uninstalls the package, you can verify it by looking at the content of /node_modules/ directory or type the following command −
$ npm ls
Search a Module
Search a package name using NPM.
$ npm search express
Create a Module
Creating a module requires package.json to be generated. Let’s generate package.json using NPM, which will generate the basic skeleton of the package.json.
$ npm init
This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sane defaults. See ‘npm help json’ for definitive documentation on these fields and exactly what they do. Use ‘npm install <pkg> –save’ afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit.
You will need to provide all the required information about your module. You can take help from the above-mentioned package.json file to understand the meanings of various information demanded. Once package.json is generated, use the following command to register yourself with NPM repository site using a valid email address.
$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) [email protected]
It is time now to publish your module −
$ npm publish
If everything is fine with your module, then it will be published in the repository and will be accessible to install using NPM like any other Node.js module.