How to Get Started with NPM (Node Package Manager)

How to Get Started with NPM (Node Package Manager)

Getting started with NPM (Node Package Manager), we are going to discuss what is NPM and how can we use it in our project. We’re going to look at the basics of working with

In this article, we are going to discuss what is NPM (Node Package Manager) and how can we use it in our project.

NPM Overview:

So what exactly is Node Package Manager?

The package is a piece of code that is managed by the Package Manager which is nothing but software that manages the installation and updating of packages.

NPM as per the official document: “NPM is the world’s largest software registry. Open-source developers from every continent use npm to share and borrow packages and many organizations use npm to manage private development as well.”

So, in NPM we use somebody else’s open-source code managed by NPM by importing into our project.

NPM usually comes with Node JS when you download it. You can check if npm is installed on your machine by simply running the command npm -v on your command prompt. if it returns some version number, it means NPM is successfully installed.

NPM has its registry at npmjs.com where we can discover packages that we can use.

Let’s look at one of the packages called chalk which is basically used for terminal styling.

Image for post

_pic credit [npmjs.com_](http://npmjs.com/)

In the above figure, we can see the weekly downloads of the package which suggests how popular is it.

Also, you can see this package dependencies in it. So this module which will serve as a dependency on our project is itself dependent on other modules. So this entire management process is taken care of by the Package Manager.

Even the source code is which is present on GitHub is given to us. We can navigate to it and verify if there are any open issues present.

One more thing before moving forward, the NPM packages come in different versions. The pattern which the version follows is semantic versioning.

As you can see, the latest version of the chalk module as and when I wrote is article is 4.1.0

It follows the semantic versioning as Major_changes**.**Minor_changes**.**Patch

Major_changes as the name stands, the significant changes made on the module which might affect your existing code.

Minor_changes are a new enhancement or feature along with defect fixes been added which should not affect your existing code.

Patch is the small bug fixes that will not crash your existing code.

You learn more about semantic versioning on semver.org

NPM Installation

Now to import any package from NPM, you first need to initialize NPM on your local project folder by running the command on the command prompt

npm init

Once you run the above command, it will ask you for some data as shown below such as package name, version, and so on.

Much of this data can be kept as default as mention in the Round brackets (). Also, the field such as author and license is for the folks who creating NPM packages.

On the other hand, we are just importing and using them to create our own application.

package name: (code_npm) code_npm
version: (1.0.0) 1.0.0
description: npm demo
entry point: (index.js) index.js
test command: test
git repository:
keywords: npm test
author: Sarvesh
license: (ISC)

Once you enter all the fields, it will create a JSON file with the values having the above properties and will ask you for confirmation as

Is this OK? (yes) yes

Once you confirmed yes it will create a package.json file with all the data you entered as illustrated below.

{
  "name": "code_npm",
  "version": "1.0.0",
  "description": "npm demo",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "npm",
    "test"
  ],
  "author": "Sarvesh",
  "license": "ISC"
}

Also, you can observe a script Object having a test property been added, which you can run using the npm test command and it will give back the desired output as

"Error: no test specified"

Now instead of doing this elongated method of initializing NPM and entering the custom properties values, you can simply run the command

npm init -y

Once you run this command, it will directly create a package.json file with the default values.

Image for post

Now to install the latest version of the chalk package in your project you need to execute the command.

npm install chalk

You can also install any specific version you need of chalk by just adding @version number as shown below and also instead of install you can simply put the short-hand i flag which stands for installation.

npm i [email protected]

This will install two things, a node_modules folder, and a package-lock.json file.

Image for post

Also, it will add a new property called dependencies to our package.json file which contains the name of the package installed and its version.

"dependencies": {
    "chalk": "^4.0.0"
  }

The node_module folder contains the packages folder and its dependencies folders. It gets modifies as and when the npm package gets installed.

The package-lock.json contains the code which makes NPM faster and secure.

"chalk": { 
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.0.0.tgz", "integrity": "sha512-N9oWFcegS0sFr9oh1oz2d7Npos6vNoWW9HvtCg5N1KRFpUhaAhvTv5Y58g880fZaEYSNm3qDz8SU1UrGvp+n7A==",
"requires": {
        "ansi-styles": "^4.1.0", 
        "supports-color": "^7.1.0" 
          }

It mainly contains properties such as version which is the semantic version number.

The resolved property is the directory or location from which the package was fetched, here in this case it was fetched from chalk.

The integrity property is to assure, we get the same code if we install the dependency again.

The requires object property represents the dependency of the chalk package.

Note:- Do not make any changes or edit these two files node_modules and package-lock.json

web-development npm javascript programming node

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.

A Guide to Web Scraping With JavaScript and Node.js

In this JavaScript Web Scraping tutorial, we’re going to illustrate how to perform web scraping with JavaScript and Node.js. Since JavaScript is excellent at manipulating the DOM (Document Object Model) inside a web browser, creating data extraction scripts in Node.js can be extremely versatile. We’ll start by demonstrating how to use the Axios and Cheerio packages to extract data from a simple website. Then, we’ll show how to use a headless browser, Puppeteer, to retrieve data from a dynamic website that loads content via JavaScript.

The Ultimate Guide to Web Scraping With JavaScript and Node.js

We’re going to illustrate how to perform web scraping with JavaScript and Node.js. Learn how to use the Axios and Cheerio packages to extract data from a simple website. Learn how to use a headless browser, Puppeteer, to retrieve data from a dynamic website that loads content via JavaScript.

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...