Using npm with Node.js

1. Introduction to Node Package Manager(npm)

Node Package Manager is a:

  1. Large Repository for various packages / modules
  2. A client to manage packages and dependencies,  install and deploy them.
  3. A website to browse the packages

All the reusable code can be put on the global repository for other developers to use. At the same time, we can have access to global repository for downloading, installing and accessing the packages that are created by developers from around the globe. As well as, it is a good practice for your team to build modular code in form of packages and re-use them in future for different projects.

1.1 So what does a NPM package contain?

A NPM package contains one or more JS files and folders. A folder is called a NPM package if it has a “package.json” file inside that folder. The contents of the folder which contains “package.json” is considered as the contents of the NPM Package or module.

2. Initializing your NPM package

It is always good to initialize a project as NPM package, so as to manage any dependencies your project/package may have for external packages. Also, other crucial information such as git repository, author info, etc will be also maintained in the package through descriptor file package.json.

Let’s initialize a package in NPM on the console. In this arctile, I will use command prompt for Windows. Reader can use similar commands in linux/unix environment.

E:\work\nodetutorial>mkdir firstmodule
E:\work\nodetutorial>cd firstmodule

Now let us write npm init command to initialize a package in the current folder.

E:\work\nodetutorial\firstmodule>npm init

On executing the code we shall have something like this:

E:\work\nodetutorial\firstmodule>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: (firstmodule)
version: (1.0.0)
description: My first module
entry point: (index.js)
test command:
git repository:
keywords:
author: Parth Joshi
license: (ISC)
About to write to E:\work\nodetutorial\firstmodule\package.json:

{
 "name": "firstmodule",
 "version": "1.0.0",
 "description": "My first module",
 "main": "index.js",
 "scripts": {
 "test": "echo \"Error: no test specified\" && exit 1"
 },
 "author": "Parth Joshi",
 "license": "ISC"
}


Is this ok? (yes)

The command will ask you couple of interactive questions. At the end of this procedure we should be able to have a package.json file created in this folder. Once we have this file, it means we have successfully initialized the package.

2.1 Package.json

With npm init we shall be able to have following content created in the package.json file:

{
  "name": "firstmodule",
  "version": "1.0.0",
  "description": "My first module",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Parth Joshi",
  "license": "ISC"
}

Minimum requirements for giving information in package.json is

  1. Name: a lower case string with no spaces. Dashes and underscores are allowed
  2. Version