What is package.json in Node.js?
Learn what package.json does in Node.js projects, key fields like scripts and dependencies, and how it enables reproducible installs across teams.
Expected Interview Answer
package.json is the manifest file at the root of a Node.js project that declares metadata, dependencies, scripts, and configuration needed to install, run, build, and publish the project consistently across machines.
Key fields include name, version, main/exports (entry point), scripts (npm run commands like start, test, build), dependencies (runtime packages), devDependencies (build/test-only tools), and engines (supported Node versions). npm, yarn, and pnpm all read this file to install exact dependency trees, and a companion lockfile (package-lock.json) pins exact resolved versions for reproducible installs. The 'type' field controls whether .js files are treated as CommonJS or ES modules. Publishing a package to npm also relies on package.json fields like 'files' and 'exports' to control what ships and how consumers import it.
- Single source of truth for project metadata
- Declares and versions dependencies reproducibly
- Defines reusable scripts (start, test, build)
- Controls module system via the 'type' field
- Required for publishing packages to npm
AI Mentor Explanation
package.json is like a team's official scorecard and squad sheet submitted before a match — it lists the playing eleven (dependencies), the designated roles like wicketkeeper or opener (scripts), and the format being played (module type). Any umpire or opposing team can read that single sheet and know exactly how to set up and run the match consistently.
Step-by-Step Explanation
Step 1
Create the file
Run npm init (or npm init -y) to scaffold package.json with default fields.
Step 2
Declare metadata
Set name, version, description, and main/exports entry point.
Step 3
Add dependencies
npm install <pkg> writes to dependencies; npm install -D <pkg> writes to devDependencies.
Step 4
Define scripts
Add named commands under 'scripts' (e.g. start, test, build) runnable via npm run <script>.
Step 5
Set module type and engines
Use 'type': 'module' for ESM, and 'engines' to pin supported Node versions.
Step 6
Commit the lockfile too
package-lock.json (or yarn.lock/pnpm-lock.yaml) ensures reproducible installs across machines.
What Interviewer Expects
- Explains package.json as the project manifest
- Distinguishes dependencies vs devDependencies
- Knows the scripts field and npm run usage
- Understands the 'type' field's effect on CommonJS vs ESM
- Mentions package-lock.json for reproducible installs
Common Mistakes
- Putting build/test tools in dependencies instead of devDependencies
- Deleting package-lock.json without understanding version drift risk
- Confusing 'main' with 'exports' in modern dual-package setups
- Not pinning engines, leading to compatibility issues across Node versions
Best Answer (HR Friendly)
“package.json is the configuration file that describes a Node.js project — its name, version, the external libraries it needs, and shortcut commands like 'start' or 'test'. It's what lets anyone set up and run the project the same way on any computer.”
Code Example
{
"name": "skillveris-api",
"version": "1.0.0",
"type": "module",
"main": "src/server.js",
"scripts": {
"start": "node src/server.js",
"dev": "nodemon src/server.js",
"test": "jest"
},
"dependencies": {
"express": "^4.19.2"
},
"devDependencies": {
"nodemon": "^3.1.0",
"jest": "^29.7.0"
},
"engines": {
"node": ">=18.0.0"
}
}
// npm run start -> node src/server.jsFollow-up Questions
- What's the difference between dependencies and devDependencies?
- What does the 'type' field do in package.json?
- Why is package-lock.json important for reproducible builds?
- How do 'main' and 'exports' differ for module resolution?
- How would you version a package following semantic versioning?
MCQ Practice
1. What is package.json primarily used for?
package.json is the manifest describing a Node project's metadata, dependencies, and scripts.
2. Where should testing-only tools like Jest be listed?
Tools only needed during development/testing belong in devDependencies, not the production dependencies list.
3. What does setting "type": "module" do?
The 'type' field controls whether .js files use ES module (import/export) or CommonJS (require) syntax.
Flash Cards
What is package.json? — The manifest file declaring a Node project's metadata, dependencies, and scripts.
Dependencies vs devDependencies? — dependencies are needed at runtime; devDependencies only for development/build/test.
What does the lockfile do? — Pins exact resolved dependency versions for reproducible installs across machines.
How do you run a custom script? — npm run <scriptName>, where scriptName matches a key under 'scripts'.