Definitive Guide to NPM : The Node.js Package Manager

NPM is the package manager for Javascript programming language. It is also the default package manager for Node.js.

NPM makes it really easy for developers to share and reuse code. Let’s say that you have written some code to solve a particular problem for your application, and you think that your code can be reused by other developers for solving a similar problem in their applications.

You can use npm to publish your code to npm registry so that other developers can download and use your code in their projects.

Similarly, You can also search and download reusable codes submitted by other developers and use it in your project.

These bits of reusable codes are called modules or packages. A package is nothing but a directory with one or more source files in it, along with a package.json file that contains some meta data about the package.

The package.json file contains several important information about the package including but not limited to the following -

  • name : Name of the package.
  • description : An optional description for the package
  • version : Version information in the form of major.minor.patch, ex - 1.2.0
  • main : The main entry file for the package.
  • author : Authors of this package
  • license : License information.
  • dependencies : A typical application generally depends on several other packages. All the packages that this application depends on is listed here.
  • devDependencies : Some dependencies are only required at the time of development and testing. Those dependencies are specified in this field.

Note that, not all the fields in package.json file are mandatory, but It must have at least the name and the version fields.

Installing NPM

NPM comes bundled with node.js, so you don’t need to install it separately. When we installed node.js in the previous tutorial, npm was also installed in the system.

You can verify npm installation by typing the following in your terminal -

$ npm --version
3.10.9

Initializing a Node.js Project using NPM

You can use npm init command to initialize a project with a package.json file.

Let’s see it in action! Fire up your terminal and create a new folder called hello-npm.

$ mkdir hello-npm

Now cd to hello-npm directory and type npm init. When you type npm init in your terminal, an interactive prompt will appear which will walk you through creating a package.json file.

NPM will try to guess the default values for all the fields. If you’re happy with the default values, just press enter and move forward, otherwise type the desired value for that field and press enter.

Once all the values are entered, It will ask for confirmation. Type yes, if everything looks OK to you -

$ cd hello-npm
$ npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible 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.
name: (hello-npm) 
version: (1.0.0) 
description: My first npm based application
entry point: (index.js) server.js
test command: 
git repository: 
keywords: NPM Tutorial
author: callicoder
license: (ISC) MIT
About to write to /Users/rajeevkumarsingh/hello-npm/package.json:

{
  "name": "hello-npm",
  "version": "1.0.0",
  "description": "My first NPM based application",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "NPM",
    "Tutorial"
  ],
  "author": "callicoder",
  "license": "MIT"
}


Is this ok? (yes) yes

Installing packages locally

You can install a package locally using npm install command -

npm install my-package

The npm install command will create a node_modules folder in the current directory (if it doesn’t exist already), and download the package to that directory.

If you want to install the package and also add it to the dependency list in the package.json file, then you can use --save option -

npm install my-package --save

Let’s install a package named express to our hello-npm application. Express is one of the most popular web frameworks for node.js -

npm install express --save

If you check the package.json file after running the above command, express will be listed in the dependencies field -

{
  "name": "hello-npm",
  "version": "1.0.0",
  "description": "My first NPM based application",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "NPM",
    "Tutorial"
  ],
  "author": "callicoder",
  "license": "MIT",
  "dependencies": {
    "express": "^4.15.3"
  }
}

NPM, by default, installs the latest version of the package available in the npm registry. If you want to install a specific version, you can append the version number after an @ sign with the package name -

npm install express@4.13.1 --save

Installing packages globally

By default, npm installs packages locally inside the project’s root folder. If you want to install a package globally, you can use -g or --global option.

These packages are installed in the system directory and are accessed from the command line. They can not be imported using require() statement.

In the following example, We install grunt-cli, a command line utility for grunt build system -

npm install grunt-cli -g

Once installed, you can access grunt from the command line -

$ grunt

Updating a package

You can use npm update command to update a package. It downloads and installs the latest version of the package and also modifies the package.json file with the new version number -

$ npm update express

Uninstalling a package

Uninstalling a package is really easy with npm. Just type npm uninstall package_name and you are done.

$ npm uninstall express

Searching for Packages

You can use npm search command to search for packages in the npm registry. Let’s see if there is a node.js package for working with aws -

$ npm search aws

This command searches for all the packages matching the keyword aws and gives the output in the following format -

NPM Search Output

Publishing a package to the npm registry

So, you have developed your application and it’s time to publish it to npm registry so that other developers can reuse your code and get benefited.

You can use npm publish command to publish your package to the npm registry. But first you need to register yourself with npm. You can do that by using npm adduser command.

$ npm adduser
Username: callicoder
Password: 
Email: (this IS public) callicoder@gmail.com
Logged in as callicoder on https://registry.npmjs.org/.

If you’re already registered with npm, you can use npm login command to login. After logging in, type the following command to publish your package -

$ npm publish

There is one caveat here. The name of your package must be unique, otherwise publish will fail. Once published, you can view your package by going to https://npmjs.com/package/<package_name>.

Conclusion

We covered quite a lot in this tutorial. We learned the basics of npm. We learned how to initialize, search, install, uninstall and publish node.js packages.

Thank you for reading. Please ask any questions in the comment section below.