Thursday, April 2, 2020

Node.js npm


NPM is the default package manager for Node.js packages. The npm is installed automatically when you install mode.js to your machine. A module is a document or directory in the node_modules directory that can be include and used by the Node.

Npm consists of a command-line client, and an online database containing thousands of public(free) and private(paid for) packages, generally known as npm registry.

Npm is basically used for managing several server-side dependencies automatically, keeping developers free from this process. We can manage our server-side dependencies manually but it can be very difficult to install and manage dependencies as the project size grows. There are thousands of free packages hosted at available to download and use.

Updating the npm

To check the version of the npm we can run the following command,

C:\Users\user> npm -v


To update npm in Linux and Mac machines the command is,

C:\Users\user>npm install -g npm@latest

To update npm in windows, we can use

C:\Users\user>npm install --global --production npm-windows-upgrade


A package is a set of all the files to compose a module. We can include the modules to our project using require() method. Basically, a module is a JavaScript library.

Download a Package

We can download a package automatically with ease using npm. We can run 

npm install package_name 

the command to download and install a package.

For example,

To download and install the package with name upper-case

C:\Users\user>npm install upper-case 

npm can install packages in local or global mode. 

npm installs the package in a node_modules folder in the current working directory in local mode. This location is owned by the current user. In global mode, packages are installed globally in {prefix}/lib/node_modules/ directory, that is owned by root ({prefix} is /user/ or /user/local).

How to use installed Package

Once the package is downloaded and installed with npm install command, it can be used. The package can be included by using the required method,

const upper = require('upper-case');

Lets’ create a Node.js file that will change text written in lower-case letters into all upper-case letter text, For example, we can create a file “test.js”,

const http = require('http');
const upper = require('upper-case');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write(upper.upperCase("Welcome to Node.js npm"));
console.log("Serving at http://localhost:3000");
Output must be->



Save the script as "test.js", and initiate the script:

Initiate test.js:

C:\Users\user>node test.js

If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:3000