Question presented to candidate: "Your CI pipeline uses "npm install" and it's been silently installing a slightly different dependency tree than what a developer tested locally, because the lockfile drifted. What command should CI actually use, and what exactly would it have done differently?"
What a strong answer should cover:
npm installis flexible: it can updatepackage-lock.jsonto satisfypackage.json's ranges, add new packages, and will happily proceed even if the lockfile was slightly out of sync beforehand — exactly the behavior that lets the prompt's drift happen silently.npm ciis strict: it requires an existing, genuinely in-sync lockfile, installs exactly what it specifies (no range resolution, no drift), and deletesnode_modulesfirst for a guaranteed-clean install — it will not silently paper over a mismatch.- 📌 Verified, not assumed: a real
npm ciagainst a genuinely out-of-syncpackage.json/lockfile pair failed outright with a realEUSAGEerror — the exact message:"npm cican only install packages when your package.json and package-lock.json ... are in sync", naming the specific missing package (chalk@5.6.2). A realnpm installagainst the **identical** mismatch genuinely **succeeded**, updating the lockfile — after whichnpm ci` on the now-in-sync lockfile genuinely succeeded too. - This directly answers the prompt: switching CI to
npm ciwould have made the exact drift the prompt describes fail loudly and immediately, at the point it was introduced, rather than silently installing a slightly different tree than what a developer actually tested — precisely the reproducibility guarantee CI needs. - A precise answer also names
npm ci's performance benefit as a secondary, real advantage (not the primary reason to prefer it here): skipping dependency-resolution logic in favor of installing exactly what the lockfile specifies is typically faster in CI, though the strict reproducibility verified above is the more directly interview-relevant answer to this specific prompt.
Clarifying questions expected:
- "Is the lockfile currently committed to version control at all, and is it being kept in sync as part of the normal PR workflow?" —
npm ci's strictness only helps if the lockfile itself is treated as a real, reviewed artifact. - "Does CI ever intentionally need to accept a slightly newer version within a declared range, or should it always install exactly the locked versions?" — the core philosophical choice between
npm install's flexibility andnpm ci's strictness.
Code / implementation expected: Yes — a real, complete before/after cycle (a genuine npm ci failure on a mismatch, a genuine npm install fix, then a genuine npm ci success) is the concrete, convincing proof of exactly what changes CI's behavior.