Open In App

Beginners Guide to package.json

Last Updated : 28 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A package.json is a file that is JSON format which stores some important information about our project. These information will help the Node Package manager to handle our project more efficiently . In a simpler way, package.json is a identity card to our Node projects . The important information stored by package.json is name, version, main, scripts, keywords, author and license. This article briefly covers about the package.json.

Creating package.json

A package.json file can be created in two ways . They are:

  • Using npm init
  • Writing directly into the file

Using npm init:

To create a package.json file we are using the node comment “npm init –y” .

  • Create a new folder and open it under any code editor (prefer VS Code)
  • Create a javascript file with the extension .js
  • Open the terminal and execute the below mentioned code to get package.json file.
npm init --y

Output:

Screenshot-from-2024-03-30-09-10-41-(1)-(1)-(1)

Manual writing into the file:

One can directly write into file with all the required information and can include it in the Node project.

But writing manually consumes time and human errors can be found . It is better to obtain the package.json file automatically using npm init command.

Properties of package.json

  • name” – The name property contains a string .It describes the name of the module that package.json is describing. In the above output sample, the name of the folder (gfg) is displayed
  • version” – It describes the version of package.json files and it follows semantic versioning.
  • description” – The description contains the human readable content that a developer wish to provide so that its easy to understand the working of the module . It is not necessary that it should given always . In the above output I haven’t mentioned any description , it contains an empty string.
  • main” – The main property will acts as direction for point of entry . This file ( sample.js) will get executed when someone runs our project.
  • scripts” – The scripts which needs to be included in the application to run properly
  • keywords” – It specifies the array of strings that characterises the application.
  • license” – It describes under which our project is distributed. Some example of licenses include MIT, ISC.
  • author ” – It describes the information about the author who owns the project.

Basics of package.json

The basics of package.json includes Identifying Metadata Inside package.json and Functional Metadata Inside package.json. Let’s discuss them briefly.

Identifying Metadata Inside package.json

The name property:

The name property contains a string .It describes the name of the module that package.json is describing.

Screenshot-from-2024-03-30-09-10-41-(1)-(1)-(1)-(1)

The sample output contains the name of the package.json file .

The version property:

It describes the current version of the module containing package.json file. The version property follows semantic versioning.

Screenshot-from-2024-03-30-09-10-41-(1)-(1)-(1)-(2)

Here the version property is stored as a string.

The license property:

It describes under which our project is distributed. They are used to describe the files in package.json. Some of the known license include MIT, ISC, and GPL-3.0.

Screenshot-from-2024-03-30-09-10-41-(1)-(2)

In the sample output , the package.json is covered using ISC license.

The description property:

The description contains the human readable content that a developer wish to provide so that its easy to understand the working of the module . It is not necessary that it should given always . The packages can be found easily by search tools when they are described.

Screenshot-from-2024-04-04-21-35-58

The keywords property:

It specifies the array of strings that characterises the application. Keywords can help identify a package and the packages related.

Screenshot-from-2024-04-04-21-39-10

In this sample i have included keywords like gfg , packages and properties.

Functional Metadata Inside package.json

The main property:

The main property will acts as direction for point of entry . This file ( sample.js) will get executed when someone runs our project.

Screenshot-from-2024-03-30-09-10-41-(1)-(1)-(1)-(3)

When the module is called via a require statement, the exports of the main property will be returned by the Node.

The repository property:

The repository property is an array where the project can be found. Basically when the projects stored in Github using Git the url can be returned .

Screenshot-from-2024-04-04-21-43-47

The scripts property:

The scripts which needs to be included in the application to run properly. Scripts are frequently used to test and build the needed commands to work with a module.

Screenshot-from-2024-04-04-21-48-33

To run a script we can “npm run” command.

The dependencies property:

The dependencies property denotes the list of the required modules/packages for our project. After installing a dependency, it is added to the dependencies list.

Screenshot-from-2024-04-04-21-45-48

Here we have installed the modules of express and mongoose.

Key Features of package.json

  • It stores essential information like name, version, description, and author of our Node project.
  • It allows other developer to share and use our projects.
  • It define scripts that is used for automate building and testing.
  • It provides a better workspace environment .


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads