Skip to content

Lesson 6 of 14 · 11m

The Lockfile Is the Build Contract

Semver ranges are resolution inputs; package-lock.json is the output — npm ci enforces it, npm install rewrites it.

^1.4.2 is a suggestion, not a pin

Write "left-pad": "^1.3.0" in package.json and run npm install on Monday. Run the same command on a fresh machine the following Friday. If left-pad@1.4.0 shipped on Wednesday — or if any transitive dep did — the two installs resolve differently. The caret is a resolution input; the lockfile is the resolution output. Only one of them is a contract.

Here is what package-lock.json actually records for every package in the tree: the exact version, the resolved URL it was fetched from, and the integrity — a sha512 hash of the tarball. Install from the lockfile and npm fetches that URL and verifies that hash. Yarn's yarn.lock, pnpm-lock.yaml, poetry.lock (which stores per-file hashes), Pipfile.lock, Cargo.lock, and go.sum all encode the same idea: the dependency graph, frozen, with content addresses.

npm ci is the contract enforced

npm install will happily mutate your lockfile — resolving new ranges, updating transitive versions, rewriting the tree. npm ci cannot: it deletes node_modules entirely and installs exactly what the lockfile says, failing hard if package.json and package-lock.json disagree. That failure is the feature. It means CI builds what you committed, not what the registry feels like serving today.

The equivalents exist everywhere: yarn install --frozen-lockfile (or --immutable on modern Yarn), pnpm install --frozen-lockfile (default in CI), poetry install (reads poetry.lock), cargo build --locked, bundle install --deployment, uv sync --locked. If your CI calls the mutating variant — npm install, plain yarn, pip install -r requirements.txt with unpinned ranges — you have re-randomized your dependency tree on every build.

The wrong mental model

"Lockfiles are for apps; libraries don't need them" is half right — libraries publish ranges because consumers resolve them. But your CI for the library still needs reproducibility, and more importantly the confusion runs deeper in the other direction: teams commit package.json and gitignore the lockfile, then wonder why staging and prod differ. The lockfile is the dependency list. package.json is the wishlist.

Another wrong turn: believing package-lock.json is just bookkeeping for npm's cache. It is the only record of what you actually ran. When event-stream@3.3.6 pulled in flatmap-stream@0.1.1, the malicious transitive edge was written into millions of lockfiles — npm ls flatmap-stream or a grep of the lockfile was how you found out if you were hit.

There is also a versioning subtlety worth knowing: package-lock.json has its own format versions — lockfileVersion 1 (npm 5–6), 2 (npm 7–8, backwards-compatible), 3 (npm 9+). A repo whose lockfile was generated on a different npm major can drift just from which npm runs the install. Pin the toolchain too: engines in package.json plus Corepack/packageManager, so CI and laptops resolve the same graph the same way.

Practice

  • Commit the lockfile. For applications, always. Review its diffs like source diffs — it is a diff of your deployed supply chain.
  • CI uses the frozen install. npm ci, --frozen-lockfile, --locked. Let it fail when files disagree; the failure is the audit.
  • npm install only when you intend to change the tree. Bumping a dep is npm install pkg@x locally, review the lockfile diff, commit both files together.
  • Regenerate deliberately. rm package-lock.json && npm install is how you do a planned full-tree refresh — with the diff reviewed — not something that happens incidentally in CI. Treat lockfile churn in a feature PR the way you'd treat a stranger's change to your Dockerfile.

Every supply-chain incident in this course lands through a dependency edge. The lockfile is where those edges are recorded — it's your audit log and your reproducibility guarantee in the same file. Treat it accordingly.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.