DEV Community

Cover image for A Detailed overview of NPM, Registry, CLI & it's related Components

Posted on • Updated on • Originally published at

A Detailed overview of NPM, Registry, CLI & it's related Components


In this article, we will look at an overview of what NPM is. When we usually run 'npm install', we know that certain packages are being created inside the 'node_modules' folder. Let us look at what they mean and what role they play in order to support running your web application.

We will look at the following topics in detail:

  • What is npm, Registry, and CLI?
  • How to initialize your project with NPM?
  • A detailed overview of 'package.json' and it's important properties.
  • How to install and publish your packages?
  • Version management of packages.

This article is based on my learnings from Tyler Mcginnis's course on React which you find over here.

What is NPM?

For a long time, I assumed 'NPM' meant 'Node Package Manager'. But, strangely and funnily it's not. 'npm, Inc.' is a for-profit, venture-backed company founded in 2014 and was acquired by Github in March of 2020. is behind the creation and management of the following components:

  • Node Package Manager
  • Registry
  • npm CLI

But before, we jump into the details of the above components, let us take a step back and find out what problem NPM solves.

Why do we need a Package Manager?

Whenever we write any web application, it is generally composed of individual modules that are often bundled together.


If we have to use a certain package with our application, the traditional way of doing it is using the 'script' tag as shown below:



<script src=""></script>  
<script src="libs/jquery2.1.4.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

While the above works, there are the following disadvantages using the above approach:

  1. If the CDN network is down, then your application cannot access the resource.
  2. In the JQuery import example above, while you have downloaded the library to your application. Managing version related changes are left up to us in terms of manually fetching the latest version and replacing them in our code, which is far from ideal.
  3. Finally, the biggest them of all is the order of script tags placed inside the body tag which is required in order for them to be referenced appropriately inside your application.

'npm' precisely solves the above problems and more in terms of managing the way your packages are referenced, versioning, and more.

Node Package Manager


The node package manager manages the below-mentioned points and does it well:

  • Provides a way to package your program for others to consume.
  • Provides a way to download the packages from a centralized repository.
  • Makes it easier for you to manage & switch versions.


The registry is a centralized repository that contains a list of all npm packages that have been published by others.

npm CLI

npm CLI provides you a way for you to install & publish the packages using the command-line interface.

How to install npm?

'npm' comes with Node. Hence, if you have installed NodeJS, then you should already have npm as it comes with it. You can install Node from here or if you are using Windows, I have a detailed article on how to install Node using a node version manager or 'nvm' over here.

If Node and npm are installed correctly, you should be able to run the following commands in your terminal to check which versions you have installed.

node -v # My installed version: v11.10.0
npm -v # My installed version: 6.9.0
Enter fullscreen mode Exit fullscreen mode

Now that you should have Node and npm installed successfully, we will look at how to initialize your project with NPM and the important components associated with the node package manager.

How to initialize your Project with npm?

The first step to use npm is navigating to your project folder (e.g. myProject) and run the command 'npm init' command from the command terminal.

$ root> cd myproject

$ root/myproject> npm init
Enter fullscreen mode Exit fullscreen mode

The above command will perform the initialization process and you’ll notice that you have a brand new package.json file and an empty node_modules directory.


Whenever you install a package, the source code for that package will be put inside of the node_modules directory. Then, whenever you import a module into your project that isn’t a file path, i.e. import React from 'react', your app will look to node_modules for the source.


The 'package.json' file contains all of the meta-information for your project. It contains information like the project’s name, author, description, and most importantly, the list of packages (as well as what versions) that your project depends on and how to run your project.

Here's a sample of the 'package.json' file:

   "name":"Example Project",
   "description": "An Example Project demonstrating Node Package Manager",
      "start":"webpack-dev-server --open",
      "build":"NODE_ENV='production' webpack"
Enter fullscreen mode Exit fullscreen mode

Let us look at a few of those properties in detail:


The list of packages listed under the 'dependencies' property is the ones your application needs in order to run. When you install a new package, the source for the package will be placed inside the 'node_modules' directory. The name & version of that package will automatically be added to the 'dependencies' property in your 'package.json' file.


'devDependencies' is exactly like the 'dependencies' in the sense the source will be placed inside the 'node_modules' directory. However, these 'dependencies' will only be made available to your application while running them in your development environment.

In other words, these 'dependencies' will not be required to run in the production environment.


The 'scripts' property defines the set of commands you can provide to the node package manager to run any kind of automation tasks. In the above example, we have two scripts, namely the 'start' and 'build'.

The general syntax to run any script is:

$ root > npm run [SCRIPT NAME]
Enter fullscreen mode Exit fullscreen mode

In the above example, if you run the to start the 'webpack-dev-server', you can run the script 'start' using the following command:

$ root/myproject> npm run start
Enter fullscreen mode Exit fullscreen mode

NPM Registry and How to install packages from the registry?

Now, that we've looked at the basics of 'npm' and the 'package.json'. We will look at how you can install certain packages from the 'npm registry'.

As I had introduced earlier, the registry is a centralized repository where all the packages are published and are available for downloads.

For Example, if you want to install 'react', you can run the following command:

$ root/myproject> npm install react
Enter fullscreen mode Exit fullscreen mode

The general syntax to install any package is:

$ root> npm install [PACKAGE NAME]
Enter fullscreen mode Exit fullscreen mode

Running 'npm install', will do the following things:

  • It will put the 'react' package inside of your 'node_modules' directory.
  • It will add 'react' as a property on your 'dependencies' object inside your package.json file.

Install your dependency as a Dev Dependency

In order to tell 'npm' that your dependency needs to be added only as a 'dev dependency', i.e., the package needs to be made only for development purposes, you should run the following command:

$ root> npm install webpack --save-dev
Enter fullscreen mode Exit fullscreen mode

The key differentiator is the '—save-dev' flag while running the 'npm install' command for installing the package as a dev dependency.

How to publish your package to NPM?

NPM has over a million packages as of the date and it wouldn't be possible if it wasn't easy to publish one. You need to have the following to publish a package on npm:

  1. An account on npm
  2. A package.json file with 'name', 'version', and 'main' (which points to the entry point of your package) properties.

Once you have the above, in your command line run the following commands:

$ root/myPackage > npm login

$ root/myPackage > npm publish
Enter fullscreen mode Exit fullscreen mode

That's all there's to it! There are more advanced features that you can check out their official guide.


If you remember, at the start of our post, we had discussed managing the different versions of our packages. Thankfully npm CLI provides a very nice way to manage it as well.

Typically each package on the npm registry follows semantic versioning. There are three parts to semantic versioning, major versions, minor versions, and patch versions.

For example, if your package version is v1.2.3, the following is the break-up:

  • Major Version - 1 - If you had a breaking change, you'll increment the major version.
  • Minor Version - 2 - If you had a non-breaking feature, you'll increment the minor version.
  • Patch Version - 3 - Everything else, you'll increment the patch version.

Why is this important? We want to avoid having our app break because we installed the wrong version of a package. npm gives us some tools to prevent this.

The '^' operator in your Version number

Let us look at the 'dependencies' inside your 'package.json' file again:

Enter fullscreen mode Exit fullscreen mode

You’ll notice that before each version number, there’s a little ^ or a 'carrot' symbol.

The ^ instructs npm to install the newest version of the package with the same major version.

Let's take, for example, the 'react' package released v16.9.0. After the new version of the package is released and you run 'npm install' inside your project, then that version of the package will be available to your project.

If 'react' released a v17.0.0, which means it's breaking change and you run 'npm install', the breaking change won't be installed and made available to your project.

The '~' operator in your version number

If your requirement is to only update your package only for updates for the same major and minor versions, then you'll use the '~' operator.

In the above example, if you change the 'prop-types' package to have the '~' operator:

Enter fullscreen mode Exit fullscreen mode

If you run 'npm install', a new package will only be installed if 'v15.7.3' is released. If 'v16.0.0' or 'v15.8.0' came out, neither would be installed.

Exact version

Finally, the simplest and direct use-case of having the exact version of what’s listed in your package.json file, you'll just need to remove all 'symbols' before your version number and only list only the version number as shown below:

Enter fullscreen mode Exit fullscreen mode

Now if you run 'npm install' anytime, only the version 'v15.7.2' will ever be installed.


A quick summary of what we discussed in this article:

  • What is npm, Registry, and CLI?
  • How to initialize your project with NPM?
  • A detailed overview of 'package.json' and it's important properties.
  • How to install and publish your packages?
  • Version management of packages.

I hope you enjoyed the article. Don't forget to subscribe to my newsletter and connect with me on Twitter @skaytech

You will also enjoy the following articles:

Top comments (0)