How to install, update and create module using NPM

| By Webner

What is Node Package Manager (NPM), how to install, uninstall and update It? How to create module in Node JS?

NPM is a package manager for JavaScript programming language. It is basically an online shared repository where we can find, share, and reuse packages of code from thousands of developers and assemble them in powerful new ways.

NPM stands for Node Package Manager. We can compare it to the PlayStore (android). From play store we can get any type of mobile app which is developed by any android programmer from anywhere in the world.

In the same process we can get multiple package from NPM developed and published by any node js programmer and include these packages in our nodejs project to reduce the efforts or time.

How to install, uninstall and update NPM

NPM comes with Node js. When we download and install Node.js we automatically get npm installed on our computer.

We can check if Node and NPM are installed or not using these commands in the terminal:

node -v // this command is used to check =node js. If already installed then it will return the version like (v6.10.2)
npm -v  // This command checks if npm is installed or not.

NPM is separate project from node js and It needs to be updated frequently because lots of new changes are updated in the packages. So NPM provides a command to update itself.

 npm install npm@latest -g // this command will update the latest NPM version

Or we can update it directly, just download the installer from the site and run it again. The new version of Node and NPM will replace the older versions.

We can uninstall one particular module by passing its name in the command

npm uninstall -g  --save  //removes the module from node_modules and also removes it from dependencies in package.json

Or we can directly Uninstall complete Node and NPM using these steps:-
1. Open the Windows Control Panel
2. Choose the “Programs and Features” option
3. Click the “Uninstall a program” option
4. Select Node.js, and click the Uninstall link, when this process completed then restart your computer.

How to create a new module in Node Js :-

Node.js modules are a complete package that can be published to npm. For creating a new module we first need to create a package.json file. This file can be created by using terminal command.

Use npm init to create package.json. It will ask you to enter values for some fields like Name, Version, Description, main, Keywords, Author, License etc. The two required fields are ‘name’ and ‘version’. We also need to set a value for ‘main’. we can use the by default, index.js.

After entering all the fields package.json file will add some Json Data

  "name": "testpackage",
  "version": "1.0.0",
  "description": "Testing new node js package",
  "main": "index.js",
  "scripts": {
	"test": "test"
  "author": "authorName",
  "license": "ISC"

Now we need to create a file which will load when our package will be installed. So we need to create a file (by default with name index.js).

This file will contain a function which will be available to other code by using the property of exports object.

exports.displayMsg = function() {
  console.log("This message displayed from the newly created package");

Now This package needs to be published in the Npm. After publishing, we need to follow these steps to install and use this module in our node js project.

1. We need to create a new directory outside of our project.
2. Then go the new directory (using cd)
3. Run npm install .
4. Create a test.js file which requires the package and calls the method.
5. Run node test.js. The message sent to the console.log should appear.

One comment

  1. Pingback: How to update NPM?

Leave a Reply

Your email address will not be published. Required fields are marked *