Article About NPM

Article About NPM

All about Node Package Manager

Why is their need of NPM

As a developer or programmer we build several websites or applications, their we are most probably need to use external libraries or dependencies. We can develop everything from the scratch, we could write our code from the scratch but we are living in the world where everything is faster also their are certain things already present. For example

  1. If anyone finds a way to connect with the database so by yourself you can build an api which will help you to connect your Application with the database.
  2. May be we want to do some validation for the objects. so lets say if we have an object which is having some properties, and you want to validate it. so one way is that you will build your module by yourself which will validate your objects. so what is the point if we have common problem and some one has already build those similar modules.
  3. So why can't we use those modules which is already present.

    If we talk about the Node.js, it has already in-build modules. For Example Http, FS(File System), Cryptography etc

What if you want extra module or want to use someone else Module ?

That is where we have the concept of NPM which is Node Package Manager so basically you can fetch them further install in your machine and use them.

You can also push your packages, let's say if you have a your module that you have created, you can push on to your github and entire world can use it.


NPM is now part of GITHUB and GITHUB is a part of Microsoft

How to get Module on your project, for example Express?

When you install node.js in your system you also get npm as well.

  • So step 1, just open terminal and write npm install express this is a command used to install package of Express. Once get installed you can use with your project.

  • After installation how to use it is the big question. Actually the way we have used the http and http is the in-built module in node.js so we don't need to download from the npm.

  • we can simply use var http = require(`http`)
  • After installing express framework now, one can use var express = require(`express`)

In case of specific version you are looking for the you have to write the particular version number before installing the particular package for example if anyone wants to install express version 4.15 then he has to go with this below code.

npm i express@4.15.1

Express is a fast, unopinionated and minimalist Web framework for Node.js. It is a "Server-Side" or "back-end" framework. It is not comparable to client-side frameworks like React, Angular, and Vue. It canbe used with those combination with those frameworks to build Full Stack Applications.


Reason behind why Node.Js is so famous?

Last year we have got more than 1M packages on NPM. That simply means we can use all those modules to build our application. Modules we can define as a small Encapsulated Unit, which can we reuse or share with anyone and which are easier to maintain

  • To simplify we can think that if we have a big problem and to solve those problem we need to break down those problem into small problem and each problem will definately have small solution and we can name those solution as a module and further you can break down those small module into small part.
  • And the advantage to make that into small part is that now it will be easier to maintain because it has less code and it is easier to share, eaiser to debug.
  • These concept in other technologies is called modularity.
  • For example you can create a modules which can connect to database, you can also create a modules using that you can write data to file, maybe you can create a module which will be good for calculations, so may be you can get a small module calculation which will have addition, substraction, division, multiplication and you could use in any of your project.
  • You could share those module with everyone in this world and they can use it. All you need is that you have to publish your module to the NPM and even if you don't want to publish you can reuse your own modules.

Http module

It is in-build module in node.js so, if we want to use that module then we have to use var http = require('http')


Creating your own modules process

we have created two files app.js and calc.js

calc.js

//calc.js
function add(a,b){
return a+b
module.exports.add = add // we are exporting function add to outside so that it could be accesed from outside
// or it can be written as 
// exports.add = add 
}

// In this file we have created function to add 2 numbers.

app.js

var calc = require("./calc")
result = calc.add(4,5)
console.log("The output is " + result)

So, basically calc is an object or reference for the module and further you are calling add function. So that's how one can use module