What is Node Package Manager? How to Use It? When to Use It?

Node Package Manager

Node Package Manager is commonly referred to as NPM. Node Package Manager comes along with NodeJS. If you have installed NodeJS, then you also have Node Package Manager (NPM) installed. Like the name suggests, it is a package manager. It can be run from the Terminal for Mac users/Command Prompt for Windows users.

What is a Package?

A package is a library of code that is designed to do a particular task. Many a times programmers use packages developed by other programmers to build applications faster. To install a package Node Package Manager (NPM) can be used. The syntax id super easy. It is as follows:

npm install <Package Name>

<Package Name> must be replaced with the name of the package that you want to install. Package.json is something that is required to work on NodeJS. This file is like settings file or config file for your whole project. One of the things that you required to add in the package.json file is other dependencies.

Let us now see what is npm init?

npm init

npm init command helps you to create a package.json and then allows you to set it’s values. See the following example for how to create package.json

npm init
name: (example_folder) npm_demo
version: (1.0.0)
description: I am demonstrating npm
entry point: (index.js) bot.js
test command:
git repository:
keywords: awkward programmer
author: vinit
license: (ISC)

This is how your package.json file gets created.

Note: Spaces are not allowed in the name of the project.

Where to find packages?

You can find a huge repository of node packages on npmjs.com. Simply search using a keyword and you will find related packages.

How to find the package that will work for you?

Simply browse through the packages listed in the search result. Take clues from last updated, version numbers, you can visit the github repository and see the number of commits to judge if the package is good or not. Or simply ask a friend or google it.

Let us see an example by using a package called Twit.

npm install twit --save

With –save as an argument to “npm install” a reference is saved to this particular package in package.json file. Once the command is executed, you can see twit listed under dependencies in package.json, and a new folder called node_modules is created. Inside node_modules, you will find twit folder which will have several folders named examples, lib, node_modules, tests, etc.

With the documentation of the package that you installed, you can learn how to use the functions of the package.

Hope it is clear now what Node Package Manager is? How to use it? What changes it brings along? What is the purpose of it? Leave me a comment if you have questions/queries/doubt.

Be the first to get notified of our latest articles and stay ahead of the competition. No need to share the email address. Subscribe with just a click!

POPULAR ARTICLE | MongoDB — Introduction Tutorial for Startups | Starting up with MongoDB