The package.json file contains important information about a Node.js project, including its name, version, scripts, and dependencies.
When we create a Node.js application, we often use packages created
by other developers. The package.json file helps us
describe our project and manage the packages it needs.
package.json file uses
JSON (JavaScript Object Notation) format. Property names and string
values must use double quotes.
package.json is a configuration file used by NPM
to store project details, dependency information, and commands
that can be run using NPM.
Open your project folder in a terminal and run the following command:
npm init
NPM asks questions about your project, such as its name, version,
description, and entry point. Your answers are used to create
the package.json file.
To create a default package.json without answering
each question, run:
npm init -y
MyNodeProject/
├── package.json
└── app.js
A simple package.json file might look like this:
{
"name": "my-node-project",
"version": "1.0.0",
"description": "My first Node.js project",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"author": "Student",
"license": "ISC"
}
This is an example configuration. Your file may contain different values depending on how the project was initialized.
The name property specifies the project or package name.
"name": "my-node-project"
The version property specifies the current
version of the project or package.
"version": "1.0.0"
The description property provides a short
explanation of the project.
"description": "A simple Node.js application"
The main property traditionally identifies
the package entry point used by tools and package consumers.
It does not automatically run your application.
"main": "app.js"
The scripts property defines commands that
can be executed using NPM.
"scripts": {
"start": "node app.js"
}
Run the script with:
npm start
The dependencies property lists packages
required by the application at runtime.
"dependencies": {
"lodash": "^4.17.21"
}
The devDependencies property lists packages
mainly used during development, testing, or building.
"devDependencies": {
"nodemon": "^3.1.0"
}
The version shown is an illustrative example.
The author property identifies the author.
The license property describes the package's
license identifier.
"author": "Student",
"license": "ISC"
Let's install Lodash in the project:
npm install lodash
NPM installs the package and records it in the
dependencies section of package.json.
{
"name": "my-node-project",
"version": "1.0.0",
"dependencies": {
"lodash": "^4.17.21"
}
}
The exact package version and other properties in your file may differ.
Install Nodemon as a development dependency:
npm install --save-dev nodemon
NPM adds Nodemon to the devDependencies section.
Nodemon is useful during development because it can restart a
Node.js process when watched files change.
Suppose your project contains a file named app.js
with the following code:
console.log("Welcome to Node.js");
Add the following script to the scripts section
of package.json:
"scripts": {
"start": "node app.js"
}
Run the script from your project directory:
npm start
Welcome to Node.js
start script has special
behavior in NPM. For other custom scripts, use commands such as
npm run dev or npm run test.
| package.json | package-lock.json |
|---|---|
| Stores project metadata and dependency declarations. | Records the resolved dependency tree and package versions. |
| Can contain scripts and other configuration. | Helps reproduce dependency installations consistently. |
| Often edited to change project configuration. | Usually maintained automatically by NPM. |
Incorrect JSON with a trailing comma:
{
"name": "my-project",
"version": "1.0.0",
}
Correct JSON:
{
"name": "my-project",
"version": "1.0.0"
}
JSON property names and string values require double quotes.
{
"name": "my-project"
}
If your script is named start, use
npm start. If it is named dev,
use npm run dev.
MyNodeProject.npm init -y.package.json.app.js file.start script that runs node app.js.npm start.package.json.package.json describes your project, dependencies,
and scripts. NPM uses it to help manage your Node.js application.
1. What is the main purpose of package.json?
2. Which command quickly creates a default package.json?
3. Which property defines custom NPM scripts?