What is package.json and what does it contain?
Short answer
package.json is the main file of every Node.js project. It lists the project's dependencies (libraries from npm), its scripts (commands like start, test and build) and basic information such as name and version. It's the Node.js equivalent of Maven's pom.xml.
Example
{
"name": "orders-api",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "jest",
"build": "tsc"
},
"dependencies": {
"express": "^4.19.2",
"pg": "^8.12.0"
},
"devDependencies": {
"jest": "^29.7.0",
"nodemon": "^3.1.0"
}
}
| Field | Meaning |
|---|---|
scripts |
Named commands. npm start runs node server.js; npm test runs jest; other names use npm run <name> |
dependencies |
Libraries the app needs to run in production (Express, database drivers) |
devDependencies |
Libraries needed only for development and testing (Jest, nodemon) — not needed in production |
"^4.19.2" |
Version range: the ^ allows newer minor versions (4.20, 4.21) but not 5.0 |
And package-lock.json?
package-lock.json records the exact version of every library that was installed, including sub-dependencies. Commit it to Git. It guarantees the pipeline installs exactly what the developer tested, even if newer versions were released since.
What to say in the interview
"package.json defines a Node.js project: its dependencies, devDependencies and scripts such as start, test and build. package-lock.json pins the exact versions of everything installed, so builds are reproducible. In the pipeline we run npm ci, which installs exactly what's in the lock file, then npm test and npm run build, and we start the app with npm start or node server.js."
Follow-up questions you may get
- Where do packages get installed? In the
node_modules/folder, which should be in.gitignore. - How do you install only production dependencies?
npm ci --omit=dev.