package.json in Node.js
The package.json file is an important configuration file in Node.js projects. It stores project information, dependencies, development dependencies, scripts, and other settings used to manage an application.
Whenever we create a Node.js project using NPM, we can create a package.json file to describe the project and manage the packages required by the application.
1. What Is package.json?
package.json is a JSON-formatted file that contains metadata and configuration for a Node.js project.
It can store information such as:
- Project name and version.
- Project description.
- Main application entry point.
- Installed dependencies.
- Development dependencies.
- Scripts used to run project commands.
Application source code
Project metadata, scripts, and dependencies
Uses project configuration to manage packages and scripts
2. Create a package.json File
First, create a folder for your Node.js project and open a terminal inside that folder.
Step 1: Create the project folder
mkdir StudentApp
cd StudentApp
Step 2: Initialize the project
npm init -y
The npm init command can guide you through project
setup. Adding -y accepts the default answers and
creates package.json without asking each question.
Example package.json
{
"name": "studentapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
The generated values may differ depending on your NPM version and the folder name.
3. Important Properties of package.json
| Property | Purpose | Example |
|---|---|---|
name |
Specifies the project or package name. | "studentapp" |
version |
Specifies the project version. | "1.0.0" |
description |
Describes the project. | "Student management app" |
main |
Declares the package entry point for consumers when applicable. | "app.js" |
scripts |
Defines commands that can be run using NPM. | "start": "node app.js" |
dependencies |
Lists packages required by the application. | "express": "..." |
devDependencies |
Lists packages used for development tasks. | "nodemon": "..." |
license |
Specifies the package license. | "ISC" |
main property is not the command that starts
every Node.js application. For a project you run directly,
the start command is commonly defined inside scripts.
4. Understanding the name and version
The name property identifies the project or package.
The version property identifies its current release.
{
"name": "student-management",
"version": "1.0.0"
}
In this example:
student-managementis the project name.1.0.0is the project's version.
A package name should follow NPM's naming rules. In a typical project, the name is lowercase and avoids spaces.
5. Understanding the scripts Property
The scripts property lets developers create shortcuts
for frequently used commands.
{
"name": "studentapp",
"version": "1.0.0",
"scripts": {
"start": "node app.js",
"dev": "node app.js"
}
}
Here, the start script runs node app.js.
The dev script runs the same command.
Create app.js
console.log("Student Management Application Started!");
Run the start script
npm start
Expected output:
Student Management Application Started!
The start script has a special shorthand:
npm start. Other custom scripts can be run with
commands such as npm run dev.
6. Dependencies in package.json
Dependencies are packages that an application needs to perform its functionality. For example, a web application may use Express to create routes and handle HTTP requests.
Install Express using:
npm install express
NPM adds Express to the project's dependencies
property in package.json. The exact version recorded depends
on the package version resolved during installation.
{
"dependencies": {
"express": "^4.21.0"
}
}
The version above is only an illustrative example. Your installed version may be different.
7. Dependencies vs devDependencies
NPM separates packages needed by the application from packages used primarily during development.
| dependencies | devDependencies |
|---|---|
| Packages needed by the application at runtime. | Packages used for development and testing tasks. |
| Example: Express for an API. | Example: Nodemon for restarting during development. |
Install with npm install package-name. |
Install with npm install package-name --save-dev. |
For example, install Nodemon as a development dependency:
npm install nodemon --save-dev
NPM records Nodemon under devDependencies.
8. package.json vs package-lock.json
These two files have different responsibilities in a Node.js project.
| package.json | package-lock.json |
|---|---|
| Contains project metadata and dependency declarations. | Records the resolved dependency tree and exact versions. |
| Can contain version ranges. | Helps reproduce the same dependency resolution. |
| Can be edited to change project configuration. | Usually maintained by NPM when dependencies change. |
9. Complete Project Structure
After creating the project and installing packages, your folder may look like this:
StudentApp/
|
|-- node_modules/
|
|-- app.js
|
|-- package.json
|
|-- package-lock.json
The node_modules directory contains installed
packages. The two JSON files describe the project and its
dependency information, while app.js contains
the application's JavaScript code.
10. Common package.json Errors
| Problem | Reason | Solution |
|---|---|---|
| Invalid JSON | Missing commas, quotes, or braces. | Correct the JSON syntax. |
| Missing script | The requested script is not defined. | Add the script under scripts. |
| Module not found | The required package may not be installed. | Install the package in the project. |
| Incorrect package name | The name may violate package naming rules. | Use a valid project name. |
11. Practice Tasks
- Create a project named EmployeeApp.
- Generate package.json using npm init -y.
- Change the project description.
- Create app.js and print a welcome message.
- Add a start script to package.json.
- Install Express as a dependency.
- Install Nodemon as a development dependency.
Summary
The package.json file stores important information about a Node.js project, including its name, version, scripts, and dependencies. The npm init -y command creates a default package.json file, while NPM commands can install and remove packages. The dependencies and devDependencies properties separate application packages from development tools. The package-lock.json file records the resolved dependency tree. Understanding package.json helps developers organize, configure, and manage Node.js projects effectively at CIIT TRAINING INSTITUTE.