Article image Understanding the Node Package Manager (NPM): Using third-party packages

4.10. Understanding the Node Package Manager (NPM): Using third-party packages

Page 27 | Listen in audio

The Node Package Manager, better known as NPM, is an indispensable tool when it comes to NodeJS development. It is the default package manager for NodeJS and is used to install, update and remove packages as well as manage project dependencies. In this section, we'll better understand what NPM is and how to use it to utilize third-party packages in our NodeJS projects.

First, what is a package? In the context of NPM, a package is simply a set of code encapsulated in a format that can be easily shared and reused. Packages can be anything from simple libraries of functions to complete frameworks. They are a way to share and reuse code, which is a common and encouraged practice in the software development community.

NPM provides an online repository where packages can be published and shared with the community. This repository is called the NPM Registry and is where you'll get third-party packages to use in your projects. There are thousands of packages available on NPM Registry, which cover a wide variety of functionality and uses.

To start using NPM, you need to have NodeJS installed on your computer. NPM is automatically installed with NodeJS, so if you have NodeJS, you already have NPM. To verify that you have NPM installed, you can open a terminal and type the following command: npm -v. This should display the version of NPM you have installed.

Now that you know what NPM is and have verified that it's installed, let's see how to use it to install third-party packages. The basic command to install a package is npm install <package-name>. For example, if you wanted to install the express package, which is a popular framework for building web applications with NodeJS, you would type npm install express.

When installing a package, NPM downloads the package code from the NPM Registry and adds it to a directory called node_modules in your project. This is where NodeJS will look when you try to require the package in your code.

In addition to installing individual packages, NPM allows you to manage all of your project's dependencies with a file called package.json. This file lists all of your project's dependencies and their specific versions, ensuring that everyone working on the project is using the same package versions. To create a package.json file, you can use the command npm init.

Once you have a package.json file, you can install all of the project's dependencies at once with the npm install command. This command reads the package.json file, downloads all listed dependencies and installs them in the node_modules directory.

In addition to managing dependencies, NPM can also be used to run scripts. These scripts can be anything from build tasks to tests or deployments. The scripts are defined in the package.json file and can be run with the command npm run <script-name>.

In short, NPM is a powerful tool that makes it easy to manage packages and dependencies in NodeJS projects. It lets you reuse third-party code easily and efficiently, and it provides a standardized way to manage and share your own code. With NPM, you can focus more on writing your own code and less on managing others' code.

Now answer the exercise about the content:

What is NPM and what is its role in NodeJS development?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Understanding the Node Package Manager (NPM): Configuring scripts in package.json

Next page of the Free Ebook:

28Understanding the Node Package Manager (NPM): Configuring scripts in package.json

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text