The Wayback Machine - https://web.archive.org/web/20241126231944/https://www.geeksforgeeks.org/beginners-guide-to-package-json/
Open In App

Beginners Guide to package.json

Last Updated : 28 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 .

Similar Reads

Difference between package.json and package-lock.json files
In Node, package.json file contains the list of dependencies and scripts in a project while the package.lock.json specifies their respective versions to ensure consistent installations in different environments. In this article, we will learn the major differences between package.json and package.lock.json and their needs in Node. Table of Content
4 min read
Why does Vite create multiple TypeScript config files: tsconfig.json, tsconfig.app.json and tsconfig.node.json?
In Vite TypeScript projects you may have noticed three typescript configuration files, tsconfig.json, tsconfig.app.json, and tsconfig.node.json. Each file is used for a different purpose and understanding their use cases can help you manage TypeScript configurations for your project. In this article, you will learn about the three TypeScript config
6 min read
How to update dependency in package.json file ?
In this article, we will discuss how to update the dependencies of a project with npm. You must have heard about npm which is called a node package manager. So, we can run this command to install an npm package. npm install Note: The --save flag is no longer needed after the Node 5.0.0 version. The package gets installed and should be found in the
3 min read
How to automatically build the package.json file for Node.js projects?
In this article, we will be discussing how to build a package.json file for Node.js Projects. At first, we need to use a code editor for our node.js project. There are various code editors available such as sublime text, Visual Studio, etc. We can use any one of them for our project. What is a package.json file? A package.json file is a JSON file t
3 min read
How to add a non-npm dependency to package.json?
One of the great features of the npm ecosystem is the ability to install and manage packages from the npm registry. These dependencies are listed in the "dependencies" section of the project's package.json file. However, sometimes you may need to use a dependency that isn't available through npm, such as a library that you've created yourself, or o
5 min read
Is there a way to force npm to generate package-lock.json?
The package-lock.json file in npm is used for ensuring consistent installations of dependencies across different environments or for collaborative projects. package-lock.json is created for locking the dependency with the installed version. It will install the exact latest version of that package in your application and save it in package.json. In
1 min read
Plugin "react" was conflicted between "package.json » eslint-config-react-app
The error Plugin "react" was conflicted between package.json and eslint-config-react-app generally arises when there is a conflict in naming convention or package.json data. However, this is not the only reason for this conflict. Some of the other reasons can be due to a different version or conflict in project dependencies. Error image from Termin
2 min read
What is the package.json file in a Node project?
In the NodeJS ecosystem, the package.json file serves as a central hub for managing dependencies, scripts, metadata, and other essential project information. It is a JSON (JavaScript Object Notation) file located at the root directory of a NodeJS project, providing vital configuration details and facilitating seamless project management. Purpose of
2 min read
How To Use Node Modules with npm and package.json
NodeJS is a powerful runtime for server-side JavaScript & these modules are reusable pieces of code that can be easily imported and used in NodeJS applications. npm (Node Package Manager) is the default package manager for Node JS and is used to install, manage, and publish NodeJS packages. This article will guide you through the process of usi
3 min read
What is package-lock.json ?
package-lock.json is a file that is generated when we try to install the node. It is generated by the Node Package Manager(npm). package-lock.json will ensure that the same versions of packages are installed. It contains the name, dependencies, and locked version of the project. It will check that same versions are installed for the different users
3 min read
Understanding & Managing Dependencies in package.json File
In the world of modern web development, managing dependencies is a crucial aspect of building robust and efficient applications. Whether you are working on a small personal project or a large-scale enterprise application, understanding and effectively managing dependencies is essential for maintaining project stability, security, and scalability. O
4 min read
How to resolve error "npm WARN package.json: No repository field" ?
The warning "npm WARN package.json: No repository field" indicates that the package.json file in your project lacks a "repository" field. The "repository" field is crucial for providing information about the version-controlled source code repository of your project. While not mandatory, including this field is recommended to enhance project documen
3 min read
What is package.json in Node.js ?
In the world of Node.js development, package.json is a crucial file that serves as the heart of any Node.js project. It acts as a manifest that defines the project’s metadata, dependencies, scripts, and more. This article will provide an in-depth look at what package.json is, why it's essential, and how to use it effectively in your Node.js project
4 min read
How to create a REST API using json-server npm package ?
This article describes how to use the json-server package as a fully working REST API. What is json-server? json-server is an npm(Node Package Manager) module/package, used for creating a REST API effortlessly. Data is communicated in JSON(JavaScript Object Notation) format between client and server. Installation: Execute the below command in your
4 min read
Difference between tilde ( ~ ) and caret ( ^ ) in package.json
In package.json, the tilde (~) and caret (^) symbols are used to specify the version range for dependencies, controlling how updates are handled when you run npm install or yarn install. Tilde allows only the patch version upgrades avoiding the minor updates while caret allows updates to patch as well as minor versions. Table of Content Tilde (~) n
3 min read
Node.js package.json
The package.json file is the heart of Node.js system. It is the manifest file of any Node.js project and contains the metadata of the project. The package.json file is the essential part to understand, learn and work with the Node.js. It is the first step to learn about development in Node.js. What does package.json file consist of?The package.json
4 min read
How to Define the Required Node.js Version in package.json?
Like the other project dependencies we can also define the node js version for a project. As we know that node is javascript runtime so it will not be contained by the normal dependencies. It ensures that the project should run and install only the compaitible node and npm version. ApproachThe define the required Node js version in the package.json
3 min read
How to Add a Custom Script to package.json that Runs a JavaScript File?
To add a custom script to your package.json file that runs a JavaScript file, you can create a script entry under the scripts section of the package.json file. This allows you to easily execute the JavaScript file using the npm run command. Steps to Add a Custom Script to package.json:Open Your package.json File: If you don’t have a package.json fi
2 min read
JSON Modify an Array Value of a JSON Object
The arrays in JSON (JavaScript Object Notation) are similar to arrays in JavaScript. Arrays in JSON can have values of the following types: nullbooleannumberstringarrayobject The arrays in JavaScript can have all these but it can also have other valid JavaScript expressions which are not allowed in JSON. The array value of a JSON object can be modi
2 min read
How to parse JSON object using JSON.stringify() in JavaScript ?
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: JSON.stringify(object, replacer, space); Paramete
2 min read
How to transforms JSON object to pretty-printed JSON using Angular Pipe ?
JSON stands for JavaScript Object Notation. It is a format for structuring data. This format is used by different web applications to communicate with each other. In this article, we will see How to use pipe to transform a JSON object to pretty-printed JSON using Angular Pipe. It meaning that it will take a JSON object string and return it pretty-p
3 min read
What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ?
JSON.parse() converts JSON strings to JavaScript objects, while JSON.stringify() converts JavaScript objects to JSON strings. JavaScript utilizes JSON for data interchange between servers and web pages. These methods enable easy data manipulation and transport in web development. JSON.parse() MethodJSON.parse() converts a JSON string to a JavaScrip
2 min read
How to Write Postman Test to Compare the Response JSON against another JSON?
In the era of API-driven development, testing the API responses to ensure their correctness is of paramount importance. One such test includes comparing the JSON response of an API against a predefined JSON. In this article, we will delve into how to write a Postman test for this purpose. Table of Content What is Postman?JSON and its Role in APIsWh
4 min read
How to use cURL to Get JSON Data and Decode JSON Data in PHP ?
In this article, we are going to see how to use cURL to Get JSON data and Decode JSON data in PHP. cURL: It stands for Client URL.It is a command line tool for sending and getting files using URL syntax.cURL allows communicating with other servers using HTTP, FTP, Telnet, and more.Approach: We are going to fetch JSON data from one of free website,
2 min read
Convert JSON String to Array of JSON Objects in JavaScript
Converting a JSON string to an array of JSON objects in JavaScript involves transforming a structured text format (JSON) into a usable JavaScript array. This allows developers to work directly with the data, enabling easier manipulation, analysis, and display of information. There are two approaches to solving this problem, which are discussed belo
2 min read
Bootstrap: A Beginners Guide
Bootstrap is a popular open-source front-end framework for developing responsive and mobile-first websites and web applications. It provides a collection of pre-built design templates, CSS and JavaScript components, and other tools, making it easier and faster to create visually appealing and functional web projects. HistoryBootstrap was originally
2 min read
Getting Started With ReactJS: A Complete Guide For Beginners
Every front-end developer and web developer knows how frustrating and painful it is to write the same code at multiple places. If they need to add a button on multiple pages they are forced to do a lot of code. Developers using other frameworks face the challenges to rework most codes even when crafting components that changed frequently. Developer
7 min read
Axios in React: A Guide for Beginners
In React, backend communication is typically achieved using the HTTP protocol. While many developers are familiar with the XML HTTP request interface and Fetch API for making HTTP requests, there's another powerful library called Axios that simplifies the process further. Table of Content Introduction to AxiosSteps to Use Axios in ReactNeed of Axio
7 min read
60 Days of Frontend Development: A Complete Guide For Beginners
Welcome to "60 Days of Frontend Development!" This structured guide targets beginners who want to learn front-end development. Within 60 days, you will learn the basics and advanced concepts, build real-world projects, and equip yourself with the skills required for beautiful and interactive websites. Frontend development is how to create a website
9 min read
100 Days of Web Development - A Complete Guide For Beginners
How to become Web Developer? What is the salary of a Web Developer? What are the skills required to become a web developer? How many days will it take to become a web developer? To answer all these questions and give you a correct pathway, we have come up with 100 Days of Web Development that will guide you day-by-day on how to become a full-stack
7 min read
three90RightbarBannerImg