Skip to solution
easyPhone Screen

What is the purpose of `package-lock.json`?

51 views
01

Understand the problem

Question presented to candidate: "Two developers run npm install against the identical package.json on different days. What guarantees they end up with the same dependency versions, and what file makes that guarantee possible?"

What a strong answer should cover:

  • package.json declares dependency ranges (^4.17.20); package-lock.json records the exact version actually resolved for every dependency and transitive dependency in the tree, plus its exact source (a tarball URL and integrity hash).
  • 📌 This is what makes installs reproducible across machines and over time — without it, two installs run on different days could resolve a range like ^4.17.20 to two different actual patch/minor versions if a new one had been published in between.
  • package-lock.json should be committed to version control — it is not a generated artifact to .gitignore, precisely because its entire value is being a shared, exact record everyone installs from.
  • npm ci (distinct from npm install) installs strictly from the lock file, deletes node_modules first, and errors out if package.json and package-lock.json are out of sync — this is why CI pipelines use npm ci, not npm install, for reproducible builds.
  • The lock file also carries an integrity hash (a checksum) for each resolved package, which npm verifies against the downloaded tarball — a defense against a compromised or tampered registry response, not just a version-pinning mechanism.
  • A precise answer distinguishes "the lock file's job" from "npm's dependency-resolution algorithm" — the lock file is the recorded result, not the resolver itself; understanding it as a recorded artifact (rather than something that itself does the resolving) avoids a common conceptual muddle.

Clarifying questions expected:

  • "Is the concern reproducibility across developer machines, or reproducibility in CI/CD specifically?" — both are solved by the same mechanism, but the answer's emphasis can differ.
  • "Yarn or pnpm instead of npm?" — each has its own equivalent lock file format (yarn.lock, pnpm-lock.yaml) serving the identical purpose.

Code / implementation expected: Optional — inspecting a real, freshly generated package-lock.json's structure (the exact version and tarball URL it records) makes the concept concrete rather than abstract.

npmdependenciespackage-lock.jsonversioning
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for Node.js phone screens — assumes basic package.json/npm familiarity. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The lock-file structure below came from **actually runnin

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Reading the real, freshly generated package-lock.json fields directly
$ npm init -y && npm install lodash@4.17.20
$ node -e "
const l = require('./package-lock.json');
console.log(Object.keys(l));
console.log('lockfileVersion:', l.lockfileVersion);
console.log('lodash version:', l.packages['node_modules/lodash'].version);
console.log('lodash resolved:', l.packages['node_modules/lodash'].resolved);
"

# [ 'name', 'version', 'lockfileVersion', 'requires', 'packages' ]
# lockfileVersion: 3
# lodash version: 4.17.20
# lodash resolved: https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz
#
# package.json only said "^4.17.20" — the EXACT version and source live here.
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 34 of 152 decoded in the Node.js track. One more won't hurt.

Back to track