npm ci fails in CI with "package.json and package-lock.json are not in sync". What do you do?
IntermediateTroubleshootingNode.js builds (npm, package.json)
Answer
Short answer
Someone changed package.json (added, removed or bumped a package) without committing the updated lockfile. npm ci refuses to guess. Fix it by running npm install locally with the same Node/npm version, committing the updated package-lock.json, and pushing.
Steps
node -v && npm -v # match the versions CI uses
npm install # regenerates package-lock.json
git add package-lock.json
git commit -m "Update lockfile"
git push
Other causes
- Different npm major versions produce different lockfile formats — pin Node in CI (
.nvmrc,engines) to match developers. - Merge conflicts in the lockfile resolved badly — delete it and regenerate with
npm install, then review the diff. - Using Yarn/pnpm locally but
npm ciin CI — use the tool that matches the committed lockfile.
Prevention
Add a PR check that runs npm ci — the PR fails before it ever reaches main.
What to say in the interview
"It means the lockfile wasn't updated with package.json. I regenerate it with npm install using the same Node version as CI and commit it. We prevent it by running npm ci on every PR and pinning the Node version."
More on Node.js builds (npm, package.json)
What is package.json and what does it contain?BeginnerHow do you build and run a Node.js application?BeginnerWhat is the difference between npm install and npm ci?BeginnerWhy must package-lock.json be committed to Git?BeginnerWhat is the difference between dependencies and devDependencies?BeginnerWhat do the ^ and ~ symbols mean in package.json versions?Beginner