Understanding package.json in NodeJS

When building a web application with NodeJS, one file that you will frequently come across in the source code is package.json. But what exactly is it, and why do we need it when building an application? In this article, we will explore everything you need to know about the package.json file.
Package.json is a special file that serves as a document that helps you understand which packages are required for this set of codes (Note: NodeJS is built on many modules called packages, which can be managed through NPM). The package.json file is a configuration file for NPM that allows it to understand what it needs to install, information about the application, version, etc.
The package.json file is written in JSON format and is located in the root directory of the project. Here’s a typical example of a package.json file:
{
"name" : "package-name",
"version" : "1.2.3"
"description" : "Application description",
"author" : "author name",
"dependencies" : {
"body-parser": "~1.5.2",
"express": "~4.7.2"
}
}
Package.json contains many attributes, but we are only interested in a few main ones.
- name: This is the name of the project or package, which should be capitalized for the attribute “name”. This is a required attribute, and if you want to publicize your project, then this attribute will be the package name, so it must be unique.
- version: This attribute specifies the version of the project. The version must consist of 3 parts: MAJOR.MINOR.PATCH
- description: This attribute provides a brief description of the project.
- author: This attribute provides information about the author.
- dependencies: This is an important one. In a project, you will have to use many packages, which have already been written; all you need to do is require them. To do this, you need to install the package through NPM. The dependencies property helps NPM understand which packages to install.
There are still many other attributes and regulations in package.json. You can learn more about them here. Understanding the package.json file is essential when working with NodeJS, as it allows you to manage the various dependencies that make up your application.