Web Programming TutorialsLearn How to Install Modules Through The NodeJS Package Manager

Learn How to Install Modules Through The NodeJS Package Manager

In this article, we are going to discuss on NodeJS Package Manager. how to install existing Node.js modules, updating, and searching of these existing Node.js modules through the Package Manager (NPM). Later in this article, we will learn about the local and global installation of packages, use of package.json file, and other useful commands to create a new Node.js module, creation of a new user to the NPM repository and publishing the new Node.js module to the repository.

Functionalities of Node Package Manager (NPM)
It has the following two core functionalities.

  • It provides online repositories for node.js packages or modules. These modules or packages can be searched through search.nodejs.org.
  • It provides the command line utility through which we can install Node.js packages, maintain version management and dependency management of Node.js packages.

NPM Version
We can check the version of NPM after executing the following command as shown below.

C:\Users\Aparajita>npm --version
2.15.8

version-of-npm
When we install Node.js on our machine which is version v0.6.3 and above, the NPM will be integrated with it. Therefore, if your machine has an older version of Node.js then it can be updated to the required version (here 2.15.8) after executing the following command.

C:\Users\Aparajita>npm install npm -g
C:\Users\Aparajita\AppData\Roaming\npm\npm -> C:\Users\Aparajita\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js
[email protected] C:\Users\Aparajita\AppData\Roaming\npm\node_modules\npm

install-node-js

How to Installing Modules using NPM
Installing new Node.js modules using NPM is very simple and can be done by executing the following command.

C:\Users\Aparajita>npm install <Module Name>

For example, if want to install the popular ‘express’ Node.js web framework then it can be easily done after executing the following command.

C:\Users\Aparajita>npm install express

install-express
When we want to use this Node.js module for our development, then we just need to import this module by using the following code.

var expressInstance = require('express');

Local vs Global Installation
Node.js modules can be installed in two ways i.e. local and global installation. The following is the comparison between both.

Local installation refers to the package on the local machine where installation takes place in the node_modules directory present inside the Node application. If packages are installed locally then these packages can be accessed through ‘require ()’ method as shown above.

Globally installed packages on the other hand have their dependencies stored in the system directory. We can use these dependencies in the CLI (Command Line Interface) function of any node.js. However, we cannot import these by using ‘require ()’ method in Node application directly.

We can install the express module as global installation by executing the following command.

C:\odesk\Abhishek Thakur>npm install express -g

After global installation, we can observe the module version (4.14.0) and the actual installation location/path (C:\Users\Aparajita\AppData\Roaming\npm) as shown below.

C:\odesk\Abhishek Thakur>npm install express -g
C:\Users\Aparajita\AppData\Roaming\npm
`-- [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]
  +-- [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]

                                                                            C:\odesk\Abhishek Thakur>

Command to check all globally installed modules

C:\odesk\Abhishek Thakur>npm ls -g

Use of package.json
The package.json is the JSON formatted file which is present at the root directory of any Node.js application or module. It is basically used to define the properties of a package. If we open the package.json file of the installed express package present at path ‘C:\Users\Aparajita\AppData\Roaming\npm\node_modules\express’ It will like this:

{
  "_args": [
    [
      {
        "raw": "express",
        "scope": null,
        "escapedName": "express",
        "name": "express",
        "rawSpec": "",
        "spec": "latest",
        "type": "tag"
      },
      "C:\\odesk\\Abhishek Thakur"
    ]
  ],
  "_from": "express@latest",
  "_id": "[email protected]",
  "_inCache": true,
  "_location": "/express",
  "_npmOperationalInternal": {
    "host": "packages-12-west.internal.npmjs.com",
    "tmp": "tmp/express-4.14.0.tgz_1466095407850_0.17484632693231106"
  },
  "_npmUser": {
    "name": "dougwilson",
    "email": "[email protected]"
  },
  "_npmVersion": "1.4.28",
  "_phantomChildren": {},
  "_requested": {
    "raw": "express",
    "scope": null,
    "escapedName": "express",
    "name": "express",
    "rawSpec": "",
    "spec": "latest",
    "type": "tag"
  },
  "_requiredBy": [
    "#USER"
  ],
  "_resolved": "https://registry.npmjs.org/express/-/express-4.14.0.tgz",
  "_shasum": "c1ee3f42cdc891fb3dc650a8922d51ec847d0d66",
  "_shrinkwrap": null,
  "_spec": "express",
  "_where": "C:\\odesk\\Abhishek Thakur",
  "author": {
    "name": "TJ Holowaychuk",
    "email": "[email protected]"
  },
  "bugs": {
    "url": "https://github.com/expressjs/express/issues"
  },
  "contributors": [
    {
      "name": "Aaron Heckmann",
      "email": "[email protected]"
    },
    {
      "name": "Ciaran Jessup",
      "email": "[email protected]"
    },
    {
      "name": "Douglas Christopher Wilson",
      "email": "[email protected]"
    },
    {
      "name": "Guillermo Rauch",
      "email": "[email protected]"
    },
    {
      "name": "Jonathan Ong",
      "email": "[email protected]"
    },
    {
      "name": "Roman Shtylman",
      "email": "[email protected]"
    },
    {
      "name": "Young Jae Sim",
      "email": "[email protected]"
    }
  ],
  "dependencies": {
    "accepts": "~1.3.3",
    "array-flatten": "1.1.1",
    "content-disposition": "0.5.1",
    "content-type": "~1.0.2",
    "cookie": "0.3.1",
    "cookie-signature": "1.0.6",
    "debug": "~2.2.0",
    "depd": "~1.1.0",
    "encodeurl": "~1.0.1",
    "escape-html": "~1.0.3",
    "etag": "~1.7.0",
    "finalhandler": "0.5.0",
    "fresh": "0.3.0",
    "merge-descriptors": "1.0.1",
    "methods": "~1.1.2",
    "on-finished": "~2.3.0",
    "parseurl": "~1.3.1",
    "path-to-regexp": "0.1.7",
    "proxy-addr": "~1.1.2",
    "qs": "6.2.0",
    "range-parser": "~1.2.0",
    "send": "0.14.1",
    "serve-static": "~1.11.1",
    "type-is": "~1.6.13",
    "utils-merge": "1.0.0",
    "vary": "~1.1.0"
  },
  "description": "Fast, unopinionated, minimalist web framework",
  "devDependencies": {
    "after": "0.8.1",
    "body-parser": "~1.15.1",
    "connect-redis": "~2.4.1",
    "cookie-parser": "~1.4.3",
    "cookie-session": "~1.2.0",
    "ejs": "2.4.2",
    "express-session": "~1.13.0",
    "istanbul": "0.4.3",
    "jade": "~1.11.0",
    "marked": "0.3.5",
    "method-override": "~2.3.6",
    "mocha": "2.5.3",
    "morgan": "~1.7.0",
    "multiparty": "~4.1.2",
    "should": "9.0.2",
    "supertest": "1.2.0",
    "vhost": "~3.0.2"
  },
  "directories": {},
  "dist": {
    "shasum": "c1ee3f42cdc891fb3dc650a8922d51ec847d0d66",
    "tarball": "https://registry.npmjs.org/express/-/express-4.14.0.tgz"
  },
  "engines": {
    "node": ">= 0.10.0"
  },
  "files": [
    "LICENSE",
    "History.md",
    "Readme.md",
    "index.js",
    "lib/"
  ],
  "gitHead": "9375a9afa9d7baa814b454c7a6818a7471aaef00",
  "homepage": "http://expressjs.com/",
  "keywords": [
    "express",
    "framework",
    "sinatra",
    "web",
    "rest",
    "restful",
    "router",
    "app",
    "api"
  ],
  "license": "MIT",
  "maintainers": [
    {
      "name": "dougwilson",
      "email": "[email protected]"
    },
    {
      "name": "hacksparrow",
      "email": "[email protected]"
    },
    {
      "name": "jasnell",
      "email": "[email protected]"
    },
    {
      "name": "mikeal",
      "email": "[email protected]"
    }
  ],
  "name": "express",
  "optionalDependencies": {},
  "readme": "ERROR: No README data found!",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/expressjs/express.git"
  },
  "scripts": {
    "test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/",
    "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/",
    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",
    "test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"
  },
  "version": "4.14.0"
}

The following are the attributes of the package.json file: –

S No

Attribute

Description

1.

name

It denotes the name of the package.

2.

version

It denotes the version of the package.

3.

description

It denotes the description of the package.

4.

homepage

It denotes the homepage of the package.

5.

author

It denotes the author of the package.

6.

contributors

It denotes the name of the contributors to the package.

7.

dependencies

It denotes the list of dependencies present in the package. All the dependencies are automatically installed by the NPM inside the node_module folder of the package.

8.

repository

It denotes the repository type and the URL of the package.

9.

main

It denotes the main entry point of the package.

10.

keywords

It denotes various keywords present in the package.

11.

maintainers

It denotes the name of the maintainers of the package.

Other useful Commands for NPM
1. Uninstalling a Node.js Module: – We can uninstall a Node.js module after executing the following command on the command line.

C:\odesk\Abhishek Thakur>npm uninstall express

2. Updating a Node.js Module: – We can update a Node.js module after executing the following command on the command line.

C:\odesk\Abhishek Thakur>npm update express

3. Searching a Node.js Module: – We can search a Node.js module after executing the following command on the command line.

C:\odesk\Abhishek Thakur>npm search express

4. Creating a Node.js Module: – We can create a Node.js module after executing the following command on the command line.

C:\odesk\Abhishek Thakur>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: (Abhishek Thakur)

5. Registration a user to NPM Repository: – We can use the following command to register ourselves with NPM repository site. The site will ask to enter a username, password and email id. Once user is created, it will be logged on to the repository (https://registry.npmjs.org/).

C:\odesk\Abhishek Thakur>npm adduser
Username: edunoix
Password:
Email: (this IS public) [email protected]
Logged in as edunoix on https://registry.npmjs.org/.

C:\odesk\Abhishek Thakur>

6. Publishing a Node.js Module to Repository: – We can publish a Node.js module after executing the following command on the command line.

C:\odesk\Abhishek Thakur>npm publish

After executing the above command, we can published our Node.js module to the repository which can be accessed to install using NPM like any other Node.js module.

Learn Nodejs by building 12 Projects From Scratch at Eduonix Courses

Source Code for Package Manager in NodeJS

Conclusion: –
In this chapter, we have covered all the basic aspects of Node package manager (NPM) required for a beginner developer to work on it.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -