What Node Package Manager?

Node Package Manager


In this article, you’ll learn what is Node’s Package Manager. the NPM enables JavaScript developers to do three main things, share their code with others, re-use their own code across projects, and use code written by others in their projects. So, NPM is basically all about code sharing and reusability.

If you want to share code with others or just re-use in other projects, NPM is the tool you need to help with that. But NPM is also about composability of bigger applications using smaller packages.

The NPM is an asset for any team that is working on any JavaScript project, it makes it easy to separate and manage the different versions of code. The NPM project runs at npmjs.com. This site hosts the many useful NPM packages that you’re going to love and appreciate.

NPM is the official Node package manager. The NPM project started with a small set of Node scripts to manage common tasks around folders that contain code for Node. and it since evolved into a fully featured package manager that is super useful for all JavaScript code, not just Node.

Node package manager

If you browse the registry of the NPM packages that are hosted on npmjs.com. you’ll find packages that are for Node and packages that are libraries and frameworks, mean to be used in a browser or a mobile application. If you see below examples of apps for robots, routers, and countless other places where JavaScript can execute. Some NPM packages represent big frameworks, like express or react.

NPM is a very important part of Node and a good understanding of how it works will prove to be very valuable for you as a Node developer. So what exactly is a package manager and why do we need it? Let’s actually start with a more basics,

What is a Package?

The Node package is basically a folder that contains scripts that can be run by Node, which is to say that any folder that has some JavaScript code in it is basically a Node package. Another name that is commonly used to describe a code folder in Node is the module. Some modules are built in Node, so NPM is not needed to manage those, but most other modules that you’ll be using are external to Node and NPM is what we can use to manage them.

When you have a project that has a lot of these code folders that we are going to start referring to as packages from now on, you’ll need to manage them somehow, especially when these packages start depending on other packages, and when you start working with multiple versions and sources of these packages. That’s where NPM can be helpful.

How NPM Command Works?

As a Node developer, you will be working with the NPM command a lot. This command comes installed with Node itself, so you don’t need to do anything to install it.

If you have Node installed, you should have the global npm command installed as well.

$ npm

You can check the version with

Node package manager version

NPM gets updated more frequently than Node, so sometimes you might need to update NPM itself separately from Node to get the latest release. You do that using the command,

install node

Let’s explore the first command of npm, the one that we just used, install. To do that, we’re going to create a test directory,

$ mkdir test-npm

$ cd test-npm

And we’re going to install one package called express.

install express package

So this NPM install command is our client that downloaded the package from its source, which by default is the npmjs.com registry itself, but you can configure the command for other sources. And after it downloaded that package, NPM placed the package in a special folder under the project named node_modules.

Package.json and Package-lock.json files

The package.json file is the one file that you’ll see in every NPM package. It’s a JSON file that can be used to provide information about a package and it’s required by NPM.

package.json file

This file is mostly modified by the NPM command itself, but in a few cases, you’ll need to manually edit this file as well. In the file, we started with a simple package.json file that only had the required properties, name and version. The name of an NPM package is its unique identifier. If you need to publish this package, that name has to be unique, and not used before, across the whole registry. The version property is a semantic versioning string.

When we installed the express dependency, NPM automatically modified our package.json and added a dependencies section documenting the version of express that it used.

Here is the package.json file for the popular express package. As you can see, this file includes meta-information about express, description, license, and keywords, but the most important information in this file is the dependencies section.

node dependencies

These are the packages that express depends on and this is the same list of packages that we got when we installed express locally in the previous test. This is really the most important benefit of the package.json file. This file makes the building of the project dependencies a reproducible task.

package.json file

Now, instead of manually creating a package.json file, you can run the NPM init command.

create package.json file

Leave a Reply

Your email address will not be published. Required fields are marked *