NPM

NPM stands for node package manager, It is a package manager for node.js applications. It helps you to install, update, remove and publish packages. Node.js package ecosystem is the world's largest ecosystem/repository of open source online libraries.

In Simple terms, you can compare npm to the PlayStore (android). From play store we can get any type of apps which is developed by any android programmer from anywhere in the whole world.

In the same way, we can get any package from NPM developed and published by any nodejs programmer and include it in our nodejs application to reduce the efforts

For example if say Bob wants a calculator in his mobile. he will simply go to the playstore and download it and use it in his android device instead of creating the whole android application.

In the same way if you want to perform say encryption in your node.js application then instead of creating the logic and coding it from scratch you just include the crypto package (which is used for encryption and stuff) from NPM and your task is done.

So npm is basically a package manager for NodeJS, with the help of which we can Find, share, and reuse packages of code from hundreds of thousands of developers — and assemble them in powerful new ways.

Installing NPM

NPM is installed automatically by downloading node.js along with Nodejs, please install nodejs if you have not installed, NodeJS installation steps

NPM Version :

You can verify installed version using below command.


npm -v

You can also check installed node version


node -v

Update NPM :

NPM updates comes more frequently as compared to node.js and it is difficult to download node.js again and again just to update npm. So we have to make sure that we are using the latest version of npm


npm install npm@latest -g

What is package

A package in Node.js contains all the files you need for a module, Similar to your linux installation or like your jar files in Java.

Download a Package :

Open the command line interface and tell NPM to download the package you want.


npm install <package-name>
// Example
npm install nodemailer

NPM creates a folder named "node_modules" in your current directory, where the package will be placed. All packages you install in the future will be placed in this folder.

nstalling specific version of a package :

If you know the exact version of the npm package you want to install, then you can download it using the package_name with @ character. The Syntax is


npm install package_name@version
// Example
npm install nodemailer@2.0.0

Removing a Package :

You can uninstall a package by using uninstall command from the npm


npm uninstall <package-name>
// Example
npm uninstall nodemailer

Install packages Globally :

You can install the packages for the user rather than installing in a specific folder, so that you can access the packages from anywhere in the system


npm install <package-name> -g   or npm install -g <package-name>
// Example
npm install nodemailer -g

UnInstall packages Globally :

uninstall the global packages


npm uninstall <package-name> -g or npm -g uninstall <package-name>
// Example
npm uninstall nodemailer

What is package.json

This file can contain a lot of meta-data about your project. But mostly it will be used for two things:

  • Managing dependencies of your project and installing not available packages
  • Scripts, that helps in generating builds, running tests and other stuff in regard to your project

Please do visit out dedicated section of Package.json

Creating a package.json file :

Run the command npm init and it will ask you some information about your project and will create a basic package.json in that directory.

You need to provide some basic details and your package.json will be ready


{
  "name": "npm-example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Install with Package.json :

If we are having a package.json file in our project, then we can install all our dependencies using just npm install command rather than installing one by one.


npm install

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions