npm, package.json and Dependencies
Beginnerpackage.json is the manifest and the lockfile is the truth. Understanding semver ranges and why the lockfile is committed prevents the "works on my machine" class of bug.
Overview
Coming from Maven or pip, npm will feel familiar in outline and different in detail. The manifest declares ranges, not versions — `^4.2.0` means anything below 5.0.0 — and the lockfile records exactly what was resolved. Committing the lockfile and installing with `npm ci` in CI is what makes builds reproducible; skipping either is how two developers end up on different code with identical repositories. The other habit worth forming early is being deliberate about what you add, because a dependency is a permanent maintenance commitment, not a free shortcut.
The Manifest
What each field does, and the dependency types that matter.
{
"name": "toolhub",
"type": "module",
"scripts": {
"dev": "next dev",
"build": "next build",
"typecheck": "tsc --noEmit"
},
"dependencies": { // shipped to the browser / needed at runtime
"next": "14.2.5", // exact
"zod": "^3.23.0" // >=3.23.0 <4.0.0
},
"devDependencies": { // build and test only, not shipped
"typescript": "~5.5.0", // >=5.5.0 <5.6.0
"vitest": "^2.0.0"
},
"engines": { "node": ">=20" }
}
// Ranges
// ^1.2.3 -> minor and patch updates (the default npm adds)
// ~1.2.3 -> patch updates only
// 1.2.3 -> exactly this
// Below 1.0.0, ^ behaves like ~ — 0.x is treated as unstable.
// Run scripts
npm run build
npx tsc --noEmit // run a binary without installing it globallyThe Lockfile
The reason two identical package.json files can install different code.
// package.json says "zod": "^3.23.0"
// You installed in January -> 3.23.1
// A teammate installs in March -> 3.24.5
// Same manifest, different code, and a bug only one of you can see.
// package-lock.json records the exact resolved tree, including
// transitive dependencies. COMMIT IT.
npm install // may update the lockfile
npm ci // installs exactly the lockfile, fails if it disagrees
// — this is what CI and Docker builds should run
// Auditing and updating deliberately
npm outdated
npm audit
npm audit fix // patch-level only
npm update zod // within the declared range
npm install zod@4 // a deliberate major, with a changelog read
// pnpm and yarn solve the same problem; pnpm additionally hard-links
// a shared store, which is much faster and disk-cheaper in a monorepo.Adding a Dependency Deliberately
The questions worth asking before npm install, because removing one later is far harder.
// Before adding, check:
// - Is it maintained? Last publish, open issue count, single maintainer?
// - How big is it? bundlephobia.com, or:
npx source-map-explorer .next/static/chunks/*.js
// - How many transitive dependencies does it drag in?
npm ls --all | wc -l
// - Could 10 lines of your own code do it?
// Things that no longer need a package:
// fetch (was axios)
// structuredClone (was lodash.clonedeep)
// crypto.randomUUID (was uuid)
// Intl.NumberFormat / Intl.DateTimeFormat (was moment)
// Array.prototype.at, groupBy, Object.groupBy
// left-pad and event-stream are the standard cautionary tales:
// every dependency is code you ship but did not review, from a
// maintainer you do not know.Key Points to Remember
- 1^1.2.3 allows minor and patch updates, ~1.2.3 allows patch only, and below 1.0.0 the caret behaves like a tilde
- 2The lockfile records the exact resolved tree — commit it, and use npm ci in CI so installs are reproducible
- 3dependencies ship at runtime; devDependencies are build and test only
- 4Every dependency is unreviewed code you ship, plus its transitive tree — check size, maintenance and alternatives first
- 5fetch, structuredClone, crypto.randomUUID and Intl have replaced several once-standard packages
Interview Questions
Sign in to ask AriaWhat is the difference between npm install and npm ci?
Why is the lockfile committed when package.json already lists the dependencies?
What would you check before adding a new dependency to a production app?
Ask Aria about npm, package.json and Dependencies
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.