Debug School

rakesh kumar
rakesh kumar

Posted on

What is NPM, and why do we need it?



What is NPM

npm is a package manager for Node.js with hundreds of thousands of packages. Although it does create some of your directory structure/organization, this is not the main purpose.

The main goal, as you touched upon, is automated dependency and package management. This means that you can specify all of your project's dependencies inside your package.json file, then any time you (or anyone else) needs to get started with your project they can just run npm install and immediately have all of the dependencies installed. On top of this, it is also possible to specify what versions your project depends upon to prevent updates from breaking your project.

It is definitely possible to manually download your libraries, copy them into the correct directories, and use them that way. However, as your project (and list of dependencies) grows, this will quickly become time-consuming and messy. It also makes collaborating and sharing your project that much more difficult.

JavaScript is almost old as the internet is and for decades was used exclusively at the client-side of applications. After years of mutations, it was to the server-side what means be responsible for business logic, processing requests from clients, interact with other services, use server hardware resources, connect to databases and more.

Do those stuff is repetitive and someone who needs to focus on the business cannot spend a lot of time building infrastructure pieces of code. And when I said pieces doesn’t mean that those are small tasks. A Database driver, for example, is a huge project per se. As the same for many available packages to create a JS application.

A decade of enthusiastic development delivered tons of pieces, packages and libraries to help compose a JS project. At that time Isaac Z. Schlueter developed the Node Package Manager to solve the cataloging and distribution of JavaScript packages as a result of having “seen module packaging done terribly”, inspired in similar projects to different languages like PHP and Perl.

Why use NPM?
Can you imagine looking on many websites, from different projects for parts to compose your project? NPM centralizes the search of those parts, takes care of versions and provides the correct download of dependencies. YES, a package could depend on another, that depends on another…

When you are ordering a meal, do you order each ingredient? And do you know the exact way to prepare it? NPM is the JS Waiter.

Image description

NPM has two important things to mention here:

1- it is an online repository for open-source javascript projects
2- it is a CLI to interact with that repository

How to use it?
NPM is part of Node.js, it means that npm Comand Line Interface will be available at the terminal/console when the Node.js is installed.

To install Node.js, follow the instructions on

The basic commands that every Node developer needs to know are to initialize, install packages, uninstall packages.


In the project-folder is necessary to create the link between the project in the local computer and the global repository.
For this the command is:

> npm init
Enter fullscreen mode Exit fullscreen mode

This command will create the package.json file with some information:

The project’s name
The project’s initial version
The project’s description
The project’s entry point (meaning the project’s main file)
The project’s test command
The project’s git repository
The project’s keywords
The project’s license
Enter fullscreen mode Exit fullscreen mode

Initially, the file looks like this:

Image description

It’s possible to edit the file, but it’s not necessary. The CLI npm was developed to manage this file.

After the initialization, it’s time to install the packages/modules that we want to aggregate to the project. For example, if it’s planned for that Node project to be a web application, the most famous package to install is “express”. For that the command is:

> npm install express
Enter fullscreen mode Exit fullscreen mode

This command will install the express module in the project, including as a dependency in the package.json file and downloading the required files.

Then, at this point, the module is ready to be used in the JS files in the project.
Each module has its way to be used but in most the cases is included using the command ‘require’:

Image description

Top comments (0)