Open In App

Beginners Guide to package.json

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:

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



npm init --y

Output:

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

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.

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.

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.

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.

The keywords property:

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

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.

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 .

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.

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.

Here we have installed the modules of express and mongoose.

Key Features of package.json


Article Tags :