Node package manager or npm was first released in 2010. In 2020, it includes around 500k open-source modules to enhance the functionality of your Node.js applications.

With regards to npm, terms package and module are used interchangeably.

How to find a package on npm

To find a package you need, you can navigate to the official npm website and type your search query into the search box.

npm-search-gui

The issue is that as of 2020, this is only useful if you know the name of the package you’re looking for.

If you have a broad query, i.e. how to change the color of output in Node.js I suggest using Google search instead.

How to install an npm package

Once you’ve found what you’re looking for, you can navigate to the description of the package.

It displays the content of the file README.md that the developer has added to the project.

Here’s an example for the module chalk. As you see, it contains installation and usage instructions.

All npm packages can be installed using the CLI tool npm and its install command.

npm install package_name

Navigate to your project directory, substitute the package_name placeholder with the actual name of the package you’re installing.

npm install chalk

This command will install the chalk module and all its dependencies.

How to install all project dependencies

In the root folder of your Node.js project, there’s a file called package.json. Among other things, it describes your project’s dependencies:

  "dependencies": {
    "chalk": "^4.1.0"
  }

The useful thing about the dependencies section is that it might contain tons of modules and you don’t have to run npm install module_name for each of them.

A single npm install in the root of your project will install all the dependencies listed in package.json.

Saving the module dependency with the -s flag

In most cases, there’s no need to edit package.json manually.

You can automatically add the dependency to it by running the installation with the -s flag or --save.

npm install --save chalk

Which is the same as

npm install -s chalk

Global installation with the -g flag

You can install the package globally using the -g flag. For the module like chalk, it doesn’t make much sense, but there are multiple other CLI tools that you might want to use globally in your system.

npm install -g firebase-tools

This will install the module firebase-tools and allow you to run the command firebase anywhere in your terminal.

Read more JavaScript tutorials or Learn Full-Stack JS from scratch!