Lesson 6 of 50 – package.json
12%
12%

Understanding package.json in Node.js

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.

Remember: The package.json file uses JSON (JavaScript Object Notation) format. Property names and string values must use double quotes.

What Is package.json?

package.json is a configuration file used by NPM to store project details, dependency information, and commands that can be run using NPM.

Main Uses of package.json
  • Stores the project name and version.
  • Describes the project.
  • Lists required packages and development packages.
  • Defines commands using the scripts property.
  • Can specify the package entry point and other metadata.

Step 1: Create package.json

Open your project folder in a terminal and run the following command:

Initialize a Node.js Project
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.

Quick Method

To create a default package.json without answering each question, run:

Command
npm init -y
Example Project Structure
Project Files
MyNodeProject/
├── package.json
└── app.js

Step 2: Understand the Structure

A simple package.json file might look like this:

package.json
{
  "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.

Important Properties of package.json

1. name

The name property specifies the project or package name.

"name": "my-node-project"
2. version

The version property specifies the current version of the project or package.

"version": "1.0.0"
3. description

The description property provides a short explanation of the project.

"description": "A simple Node.js application"
4. main

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"
5. scripts

The scripts property defines commands that can be executed using NPM.

"scripts": {
  "start": "node app.js"
}

Run the script with:

npm start
6. dependencies

The dependencies property lists packages required by the application at runtime.

"dependencies": {
  "lodash": "^4.17.21"
}
7. devDependencies

The devDependencies property lists packages mainly used during development, testing, or building.

"devDependencies": {
  "nodemon": "^3.1.0"
}

The version shown is an illustrative example.

8. author and license

The author property identifies the author. The license property describes the package's license identifier.

"author": "Student",
"license": "ISC"

Step 3: Install a Package

Let's install Lodash in the project:

Install Lodash
npm install lodash

NPM installs the package and records it in the dependencies section of package.json.

Example After Installation
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.

Step 4: Add a Development Dependency

Install Nodemon as a development dependency:

Command
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.

Step 5: Create and Run an NPM Script

Suppose your project contains a file named app.js with the following code:

app.js
console.log("Welcome to Node.js");

Add the following script to the scripts section of package.json:

package.json Script
"scripts": {
  "start": "node app.js"
}

Run the script from your project directory:

npm start
Output
Terminal Output
Welcome to Node.js
Note: The start script has special behavior in NPM. For other custom scripts, use commands such as npm run dev or npm run test.

package.json vs package-lock.json

Comparison
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.

Common Mistakes

1. Invalid JSON

Incorrect JSON with a trailing comma:

{
  "name": "my-project",
  "version": "1.0.0",
}

Correct JSON:

{
  "name": "my-project",
  "version": "1.0.0"
}
2. Using Single Quotes

JSON property names and string values require double quotes.

{
  "name": "my-project"
}
3. Incorrect Script Name

If your script is named start, use npm start. If it is named dev, use npm run dev.

Practical Assignment

Practice Tasks
  1. Create a folder named MyNodeProject.
  2. Run npm init -y.
  3. Open and examine the generated package.json.
  4. Change the project description.
  5. Create an app.js file.
  6. Add a start script that runs node app.js.
  7. Run your application using npm start.
  8. Install Lodash and observe the changes in package.json.
  9. Install Nodemon as a development dependency.
💡 Remember
package.json describes your project, dependencies, and scripts. NPM uses it to help manage your Node.js application.

🧠 Quiz

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?