NodeJs

What is “package.json” file? How to create package json file?

What is package json how to use it. what is npm init, how to create package json

In our previous Node.js tutorials, We have introduced NodeJs, npm, Express and discussed how we can create our first application using express.js. Now in this knowledge bit, we will discuss package.json. We will learn that why we use –save to install package. We will also discuss the purpose of the npm init command and we will learn how to create package json file.

What is Package.json?

The Package.json is one of the core components of the NodeJs runtime environment. It is contained by all the npm packages. It is a plain text JSON file which contains the basic information about the project.

This file is used to hold the various metadata and information about the project in the simple plain text and as a JSON file.

Why do we need to use package.json?

The package.json file is used to list down all the dependencies of the project we are working on. It is also used to specify the version of the package of the project and this package make it much easier for developers to share their project with their co-team members and other developers.

This package gives information about the project to npm which allow the developer to understand and identify the project and handle the dependencies.

The Package.Json file contains numbers of different mandatory and optional directive or fields.

Mandatory Directives

This package contains two mandatory directives:

  1. Name
  2. Version

Name:

Every Package.json must contain the name and it should need to be unique, and it must be written in lowercase letters. It must be in one word with no space. We can also use hyphens and underscores while writing the name.

Example

"name":"ourpackage" 

Version:

The version is also mandatory directive, it is node.js package version number and should be written in this form x.x.x. npm use this version number to install, uninstall, or update the right and required version in the node.js runtime environment.

"version": "1.0.0"

Example of sample package.json file:

{
"name" : " ourpackage ",
"version" : "1.0.0"
}

Optional Directives:

There are other some optional directive that we can use in our Node.Js project.

Description: here we write the description of the node.js project to provide the brief and concise description about the module.

Entry point: the entry point refers to the file where the application starts like if we want entry point of our project app.js then we write app.js. We can use whatever we want as an entry point.

Contributors: here we will write the names of all the developers and the people who have contributed to this project to give them credit.

Keywords: we can also write the keyword of the project in the Package.json file. This will allow people to find your project.

Author: here developer will write their name.

How to create package json file?

There are basically two ways to create package.json file, we can create by running following command on the command line:

> npm init

It will create a package.json file in the specified directory.

Or we can also create default package.json file using the following command:

>npm init –yes

It will extract the information from the project and automatically generate the package.json file in the specified directory.

 

how to create package JSON file

What does –save do?

When we install a package with npm install and we add this –save flag at the end then it will take the package name and version name and then automatically save it into the package.json file.

Let’s demonstrate with the example: go ahead and create a folder and in that folder open Terminal. To create package.Json file run the npm init command in your terminal. Here is the demonstration below:

 

npm init --save command purpose and uses

 

You can notice that we have name, version description but we do not have dependencies yet. Now, we are adding the express module into this folder using npm install express command and this time we will also use –save at the end of the command. It will automatically add the dependencies into our package.json file. Here is the demonstration below:

 

add dependencies in the package json file automatically using --save command

 

Now, you can notice in the package.json file that the dependencies field added to in it, and in the dependencies, you will see express and then its version number. Here is my package.json file demonstration.

 

thi is how to create Package.json file

 

If we add another package like knock-knock-jokes in our project using the command (npm install knock-knock-jokes –save) then it will also add this package under dependencies. Here is the demonstration below:

 

Create package json file - how to add module and dependencies to package json file

 

So, when we run the command, it automatically added the name and version number of the knock-knock-jokes package in dependencies of the package.json file. Here is our package.json file after added the knock knock jokes package.

 

Create package json file add multiple dependencies to package.json file

 

So, you can notice that it added the knock-knock-jokes with its version number 1.7.0 under the dependencies

Conclusion:

In this Nodejs tutorial, We have learned about package.json file and discussed mandatory and optional directive. We have also seen that how we can create package json file. Then we have discussed that why we use the npm init and –save command.

Click to comment

Most Popular

To Top