What Are Packages?

Learn about Packages in Node.js.

Packages in Node.js

Node.js packages allow us to reuse code that has already been written by other people. This concept is not something that is unique to Node.js; Python and its package installer, pip, accomplish what Node.js does with npm. So what exactly is a package?

Here are some famous packages:

  • React is a JavaScript library that allows us to create interactive user interfaces.
  • Express is a minimal Node.js web application framework that provides a robust set of features for web and mobile applications.
  • Axios is a promise-based HTTP client for the browser and Node.js.

A package must contain a package.json file in order to be published to the npm registry. Since modules are not required to have a package.json file, not all modules are packages. Only modules that have a package.json file are packages.

npm

npm is the package manager for Node.js; it is a software registry that makes sharing and accessing packages easy. npm is installed when Node.js is installed. Each project has dependencies that are listed in its package.json. The dependencies can be installed using a single line. The website makes searching for packages very easy. Packages can then be installed from the npm CLICommand Line Interface. Try installing React in the terminal below. Use the command npm install react.

Terminal 1
Terminal
Loading...

Why do we use packages?

There are quite a few benefits of using packages. Let’s take a look at a few.

Don’t reinvent the wheel

The saying, “If it’s not broken, don’t fix it,” can most certainly be applied to code. Some of the packages hosted on npm may have most of the functionality that you might be looking to implement in your own code. The Node.js http module can be used to create a simple server. However, adding additional functionality can result in a lot of repeated and, often, complicated code. Express can simplify this process and can get your work done in a fraction of the time and much less code.

Tried and tested

While it is relatively easy to submit your own package to npm, some of the packages hosted there have been around for quite some time. This allows the community to build and improve packages – often adding more features, making them more efficient, or keeping them up to date with the newest version of Node.js. Most packages provide a sense of reliability that only comes from years of continuous improvement.

svg viewer

Get hands-on with 1300+ tech skills courses.