The package.json file is the manifest file that contains various information about your Node.js project. It is the most critical file of the package. It is used by NPM to know everything about your project, such as project name, project version, core dependencies, authors, starting scripts, etc.
The package.json file is present in the root directory of the project.
Note: All the metadata of your Node.js project is there in the package.json file.
The package.json file holds two types of metadata:
You don't have to create the package.json file manually. Instead, NPM provides you the npm init
command, using which you can easily create this file for your project.
Note: Whether you plan to publish your Node.js project or not, you must have the package.json file.
Various npm commands, such as npm install
, npm uninstall
, etc., update package.json file so that this file has the most updated information about your project.
An example package.json
looks like this:
{ "name": "chatting-app", "version": "1.2.0", "description": "Chatting app using Socket.io", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node index.js" }, "repository": { "type": "git", "url": "https://www.github.com/tutorialsandyou" }, "keywords": [ "chatting app", "chat software", "socket.io chatting app" ], "author": "Mohit Natani [email protected] https://www.tutorialsandyou.com", "contributors": [{ "name": "Nidhi Gujarati", "email": "[email protected]", "url": "https://www.tutorialsandyou.com" }], "license": "MIT", "dependencies": { "express": "^4.18.1", "socket.io": "^4.5.1" }, "devDependencies": { "nodemon": "^2.0.16" } }
If you take a closer look at package.json, you will find that this file has various fields. You must be familiar with these fields so that you can manage this file in a better way. In this tutorial, we will discuss commonly used fields that you find in the package.json file. If you are interested in knowing about the rest of the fields, visit this documentation.
"name": "chatting-app"
The name
field represents the name of your package and project. It must be less than or equal to 214 characters. Only lowercase letters and URL-safe characters are allowed in the name field. If you plan to publish your package to the NPM registry, the name must be unique; otherwise, you will get an error if the package name is already taken. On the other hand, if you are not publishing the package, then the name need not be unique.
"version": "1.2.0"
The version
defines the current version of your project. Since Node.js uses semantic versioning, you must write the version number in SemVer format. When you publish your package, the version field is required, without which you can't publish the package.
To learn more about Semantic Versioning, visit What is Semantic Versioning in npm?
"description": "Chatting app using Socket.io"
The description
field provides a short summary of the package.
When you search for a package in the NPM registry, then the description is displayed along with the package name.
"keywords": [ "chatting app", "chat software", "socket.io chatting app" ]
The keywords
field is an array of strings. The keywords you mention in this field are indexed by the NPM registry, and when you search for a package, keywords are used to find packages.
"license": "MIT"
The license
field allows you to specify how you want to share your code with other people. You pass license ids to this field. For example, specify MIT
for MIT license, ISC
for ISC license, and Apache-2.0
for Apache License 2.0.
If you are confused about which license to choose, visit Choose a License website.
"author": "Mohit Natani [email protected] https://www.tutorialsandyou.com", "contributors": [{ "name": "Nidhi Gujarati", "email": "[email protected]", "url": "https://www.tutorialsandyou.com" }]
These fields list the contact details of persons involved in the project's development.
If the developer is a single person, then use the author
field. On the other hand, if a project is developed by a group of people, then use the contributors
field.
You provide value to these fields either in "Name <email> <url>"
format or an object having fields name
, email
, and url
. The email and url are optional in both formats.
"main": "index.js"
Let's understand the main
field with the help of an example.
Suppose you have a package called chatting-app
, and a user has installed this package. When the user writes require('chatting-app')
in the code, then all the properties exported by the file mentioned in the main field are returned by require.
"dependencies": { "express": "^4.18.1", "socket.io": "^4.5.1" }
The dependencies field is an object having package names as keys and a version as a value. Along with the version, you will find tilde (~), caret (^) and other symbols. These notations are the way to specify version ranges. To learn more about why tilde and caret are used in package.json, visit this link.
All the core dependencies of your project are listed in the dependencies field. It is the most important field of package.json file. In Node.js, the dependencies field is used in two ways:
npm install package-name
command, all the files and dependencies of that package are downloaded to the node_modules
folder. Along with this, an entry of that package is made to the dependencies
property. It is essential to understand that all the external packages upon which your project depends are listed in the dependencies
field.npm install
command. NPM installs all the packages listed in the dependencies field to the node_modules folder."devDependencies": { "nodemon": "^2.0.16" }
The devDependencies
field is similar to the dependencies
field, except all the packages that are needed during development and not in production are listed in the devDependencies field. For example, packages such as nodemon and eslint have no utility in the production environment and are installed as development dependencies.
To install a package as devDependencies, run npm install package-name --save-dev
command.
You can visit this tutorial to learn about --save-dev in the npm install command.
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node index.js" }
The scripts field defines terminal commands, which you can run with the npm run <script-name>
command. This field is an object with script names as keys, and the value is the command you want to run.
"repository": { "type": "git", "url": "https://www.github.com/tutorialsandyou" }
The repository field is used to specify the location of the source code and the version control system it uses. This field is an object with properties type and url. GitHub and Bitbucket are used for hosting open-source projects.
package.json
is a JSON file, so if you make changes to this file manually, then make sure it should remain a valid JSON. If you make any formatting errors, then NPM won't be able to process the package.json file. Therefore, it is recommended that you should interact with the package.json file using the npm cli commands.
If you want to learn about how to create a package.json file in Node.js, visit this link.