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.
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?
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.
You can check the version with
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,
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.
So this NPM install command is our client that downloaded the package from its source, which by default is the
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.
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.
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.
Now, instead of manually creating a package.json file, you can run the NPM init command.