Introduction
package.json is one of the most important files in a Node.js project. It stores project information, scripts, dependencies, development dependencies, version details, and other configuration settings. In this chapter, you will practice creating and editing package.json, adding scripts, managing dependencies, understanding versions, setting the main file, adding project information, and working with common package.json properties through simple step-by-step examples. Node.js package.json practice questions with solutions help to understand the concepts.
Question 1: How do you create a package.json file?
Problem
Create a new Node.js project and generate a package.json file using npm.
Solution
Create a project folder:
mkdir package-json-practice
Move into the folder:
cd package-json-practice
Now run:
npm init
npm will ask you several questions.
You can also use:
npm init -y
This creates the file using default values.
Output
A new file will be created:
package-json-practice/
└── package.json
Example package.json
{
"name": "package-json-practice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Step-by-Step Explanation
- Create a project folder.
- Open the folder in the terminal.
- Run
npm init. - npm asks for project information.
- npm creates
package.json. package.jsonbecomes the main configuration file for your npm project.
Question 2: How do you change the name and version in package.json?
Problem
Create a project named student-app with version 1.0.0.
Solution
Run:
npm init -y
Then open package.json and update it:
{
"name": "student-app",
"version": "1.0.0",
"description": "A simple Node.js student application",
"main": "index.js",
"scripts": {},
"keywords": [
"nodejs",
"student"
],
"author": "Student",
"license": "ISC"
}
Output
Your project information is now stored inside package.json.
Step-by-Step Explanation
The name property stores the project name:
"name": "student-app"
The version property stores the project version:
"version": "1.0.0"
The version usually follows:
MAJOR.MINOR.PATCH
For example:
1.0.0
Version Example
A bug fix might increase:
1.0.0 → 1.0.1
A new backward-compatible feature might increase:
1.0.1 → 1.1.0
A major breaking change might increase:
1.1.0 → 2.0.0
Question 3: How do you add a description and keywords to package.json?
Problem
Create a package.json file for a Node.js learning project and add a description and keywords.
Solution
{
"name": "node-learning",
"version": "1.0.0",
"description": "A beginner-friendly Node.js learning project",
"main": "index.js",
"keywords": [
"nodejs",
"javascript",
"backend",
"programming"
],
"author": "Student",
"license": "ISC"
}
Output
The project now contains useful information about itself.
Step-by-Step Explanation
The description property explains what the project does:
"description": "A beginner-friendly Node.js learning project"
The keywords property contains words related to the project:
"keywords": [
"nodejs",
"javascript",
"backend"
]
Keywords are stored as an array.
Question 4: What is the main property in package.json?
Problem
Create a Node.js project where index.js is the main application file.
Solution
Create index.js:
console.log("Node.js application started.");
Then configure package.json:
{
"name": "my-node-app",
"version": "1.0.0",
"main": "index.js"
}
Output
The project structure:
my-node-app/
├── index.js
└── package.json
Step-by-Step Explanation
The main property identifies the primary entry file of the package:
"main": "index.js"
If your main file is named app.js, you can write:
"main": "app.js"
Question 5: How do you add npm scripts in package.json?
Problem
Create start and dev scripts for a Node.js application.
Solution
First install Nodemon as a development dependency:
npm install --save-dev nodemon
Now add scripts:
{
"name": "my-node-app",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
"devDependencies": {
"nodemon": "^3.0.0"
}
}
The exact Nodemon version may differ on your system.
Run the Start Script
npm start
This executes:
node index.js
Run the Development Script
npm run dev
This executes:
nodemon index.js
Step-by-Step Explanation
- Create the
scriptsobject. - Add the
startscript. - Add the
devscript. npm startruns thestartscript.npm run devruns thedevscript.
Question 6: How do dependencies appear in package.json?
Problem
Install Express and understand how npm records it inside package.json.
Solution
Initialize the project:
npm init -y
Install Express:
npm install express
npm automatically adds Express to dependencies.
Your package.json will contain something similar to:
{
"name": "my-node-app",
"version": "1.0.0",
"dependencies": {
"express": "^5.0.0"
}
}
The exact version may be different when you install it.
Step-by-Step Explanation
- Run
npm init -y. - npm creates
package.json. - Run
npm install express. - npm downloads Express.
- npm adds Express to
dependencies. - npm also updates
package-lock.json.
Question 7: How do you add a development dependency to package.json?
Problem
Install Nodemon as a development dependency and understand where it appears in package.json.
Solution
Run:
npm install --save-dev nodemon
Your package.json will contain something similar to:
{
"name": "my-node-app",
"version": "1.0.0",
"scripts": {
"dev": "nodemon index.js"
},
"devDependencies": {
"nodemon": "^3.0.0"
}
}
Step-by-Step Explanation
--save-devtells npm that the package is for development.- npm installs Nodemon.
- npm adds it to
devDependencies. - You can use Nodemon through an npm script.
Dependencies
"dependencies": {
"express": "^5.0.0"
}
Development Dependencies
"devDependencies": {
"nodemon": "^3.0.0"
}
Question 8: How do you use type: module in package.json?
Problem
Configure a Node.js project to use ES Modules and allow import and export.
Solution
Add:
{
"name": "es-module-practice",
"version": "1.0.0",
"type": "module"
}
Create math.js:
export function add(a, b) {
return a + b;
}
Create index.js:
import { add } from "./math.js";
const result = add(10, 20);
console.log(result);
Run:
node index.js
Output
30
Step-by-Step Explanation
Normally, Node.js projects can use CommonJS syntax such as:
const fs = require("fs");
When you set:
"type": "module"
you can use ES Module syntax:
import fs from "fs";
and:
export function add() {
}
Important Point
When using ES Modules with local files, include the file extension:
import { add } from "./math.js";
Question 9: How do you remove a dependency from package.json?
Problem
Install Lodash and then remove it from your project.
Solution
Install Lodash:
npm install lodash
Now remove it:
npm uninstall lodash
Before
Your package.json may contain:
{
"dependencies": {
"lodash": "^4.17.21"
}
}
After
The Lodash dependency is removed:
{
"dependencies": {}
}
Depending on the other packages in your project, the dependencies section may contain other packages or may disappear from the file.
Step-by-Step Explanation
- Install the package.
- npm adds it to
dependencies. - Run
npm uninstall lodash. - npm removes the package from
node_modules. - npm removes its entry from
package.json. - npm updates
package-lock.json.
General Command
npm uninstall package-name
For example:
npm uninstall express
Question 10: How do you create a complete package.json for a Node.js project?
Problem
Create a complete Node.js project configuration containing:
- Project information
- Main file
- npm scripts
- Dependencies
- Development dependencies
- ES Module configuration
Solution
Create a project:
mkdir complete-node-project
Move into it:
cd complete-node-project
Initialize npm:
npm init -y
Install Express:
npm install express
Install Nodemon:
npm install --save-dev nodemon
Now create index.js:
import express from "express";
const app = express();
const PORT = 3000;
app.get("/", (req, res) => {
res.send("Welcome to my Node.js project!");
});
app.get("/about", (req, res) => {
res.send("This is the About page.");
});
app.listen(PORT, () => {
console.log(
`Server running at http://localhost:${PORT}`
);
});
Now configure package.json:
{
"name": "complete-node-project",
"version": "1.0.0",
"description": "A beginner Node.js project using Express",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
"keywords": [
"nodejs",
"express",
"javascript",
"backend"
],
"author": "Student",
"license": "ISC",
"dependencies": {
"express": "^5.0.0"
},
"devDependencies": {
"nodemon": "^3.0.0"
}
}
The exact Express and Nodemon versions may be different when you install them.
Run the Application
For normal execution:
npm start
For development:
npm run dev
Output
Terminal:
Server running at http://localhost:3000
Open:
http://localhost:3000/
Browser:
Welcome to my Node.js project!
Open:
http://localhost:3000/about
Browser:
This is the About page.
Step-by-Step Explanation
- Create the project folder.
- Run
npm init -y. - npm creates
package.json. - Install Express.
- Install Nodemon as a development dependency.
- Add
"type": "module"to useimport. - Set
index.jsas the main file. - Add
startanddevscripts. - Add project information.
- Start the application using
npm start. - Use
npm run devduring development. - npm keeps the project’s package information and dependencies inside
package.json.
Key Takeaways
package.jsonis a central configuration file for Node.js npm projects.npm initcreates apackage.jsonfile.npm init -ycreates one using default values.- The
nameproperty stores the project name. - The
versionproperty stores the project version. - The
descriptionproperty explains the project. - The
mainproperty identifies the package’s main entry file. - The
scriptsproperty stores commands that can be run with npm. npm startruns thestartscript.- Custom scripts can be run with
npm run script-name. dependenciescontains packages needed by the application.devDependenciescontains packages mainly used during development.keywordsstores words related to the project.authorstores author information.licensespecifies the project’s license."type": "module"enables ES Module behavior for.jsfiles in that package scope.- Installing packages with npm automatically updates
package.json. - Uninstalling a package removes its dependency entry.
package-lock.jsonrecords the resolved dependency tree and versions.package.jsonmakes it easier for other developers and deployment systems to understand and install a Node.js project.
FAQs
1. What is package.json in Node.js?
package.json is a configuration file used by Node.js npm projects.
It stores information such as:
Project name
Version
Scripts
Dependencies
DevDependencies
Main file
Description
License
It helps npm understand how the project is configured.
2. How do I create package.json?
Run:
npm init
npm will ask you questions and create the file.
For a faster setup, use:
npm init -y
3. What is the main property in package.json?
The main property identifies the main entry file of a package.
Example:
{
"main": "index.js"
}
However, the main property by itself does not create an npm start command. To start your application with npm start, define a script:
{
"scripts": {
"start": "node index.js"
}
}
4. What is the scripts section in package.json?
The scripts section contains commands that you can run using npm.
Example:
{
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
}
You can run:
npm start
or:
npm run dev
5. What is the difference between dependencies and devDependencies?
dependencies contains packages needed by the application.
Example:
"dependencies": {
"express": "^5.0.0"
}
devDependencies contains packages mainly needed during development.
Example:
"devDependencies": {
"nodemon": "^3.0.0"
}
6. What does "type": "module" do in package.json?
It tells Node.js to treat .js files in that package scope as ES Modules.
This allows syntax such as:
import express from "express";
and:
export function add(a, b) {
return a + b;
}
Without "type": "module", Node.js commonly treats .js files as CommonJS unless another module configuration applies.
7. Should package.json be included when sharing a Node.js project?
Yes. package.json should normally be included because it tells npm which packages and scripts the project uses.
The node_modules folder is normally not shared or committed to version control. Another developer can usually recreate it by running:
npm install
inside the project directory.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
