How to fix npm ERR! code EUSAGE npm ERR! `npm ci` can only install packages when your package.json and package-lock.json are in sync in npm
This error occurs when package.json has been modified without updating package-lock.json. Run npm install to regenerate the lockfile, then commit both files.
Quick fix
Fixes npm ERR! code EUSAGE npm ERR! `npm ci` can only install packages when your package.json and package-lock.json are in sync
# Delete existing lockfile and node_modules
rm -rf node_modules package-lock.json
# Regenerate
npm install- Regenerate the lockfile locally
- Verify the sync
- Commit both files together
- Prevent future issues
# Delete existing lockfile and node_modulesrm -rf node_modules package-lock.json# Regeneratenpm installnpm ERR! code EUSAGEnpm ERR! `npm ci` can only install packages when your package.json and package-lock.json are in sync
What this error means
npm ci is designed for automated environments (CI/CD) where reproducible builds are critical. It requires package.json and package-lock.json to be perfectly synchronized. Unlike npm install, which can update the lockfile, npm ci refuses to install if any dependencies in package.json don't match the lockfile. This ensures that builds are reproducible and don't accidentally introduce different versions. The error indicates someone modified package.json (added, removed, or changed a dependency version) but didn't run npm install to update the lockfile.
Typical symptoms
- CI/CD pipeline fails at npm ci step
- Error mentions "package.json and package-lock.json are in sync"
- Builds fail after adding or updating dependencies
- Works locally with npm install but fails in CI with npm ci
- Pull requests fail automated checks
Common causes
- package.json edited manually without running npm install
- Dependency version changed in package.json but lockfile not updated
- package-lock.json not committed after adding new dependency
- Merge conflict resolved incorrectly, leaving files out of sync
- Different npm versions between developers causing lockfile format differences
How to fix "npm ERR! code EUSAGE npm ERR! `npm ci` can only install packages when your package.json and package-lock.json are in sync"
On your development machine:
# Delete existing lockfile and node_modules
rm -rf node_modules package-lock.json
# Regenerate
npm installThis creates a fresh lockfile matching your current package.json.
Run npm ci locally to verify the fix:
rm -rf node_modules
npm ciIf this succeeds, the files are in sync.
Always commit package.json and package-lock.json together:
git add package.json package-lock.json
git commit -m "Fix: sync package.json and package-lock.json"
git pushAdd a pre-commit hook to prevent out-of-sync files:
# Using husky
npx husky add .husky/pre-commit "npm ci --dry-run"Or add a CI check that fails fast if files are out of sync.
Advanced notes
npm ci vs npm install:
| Feature | npm install | npm ci |
|---------|------------|--------|
| Updates lockfile | Yes | No |
| Deletes node_modules | No | Yes |
| Speed | Slower | Faster |
| Use case | Development | CI/CD |
If you need to update a single dependency without npm install:
npm update <package-name>For monorepos with workspaces, ensure all workspace package-lock.json files are in sync.
Different npm versions can generate different lockfile formats (lockfileVersion 1, 2, or 3). Standardize npm versions across your team using engines in package.json:
{
"engines": {
"npm": ">=9.0.0"
}
}Frequently asked questions
- What does "npm ERR! code EUSAGE npm ERR! `npm ci` can only install packages when" mean?
- npm ci is designed for automated environments (CI/CD) where reproducible builds are critical. json to be perfectly synchronized. json don't match the lockfile.
- How do I fix "npm ERR! code EUSAGE npm ERR! `npm ci` can only install packages when"?
- This error occurs when package.json has been modified without updating package-lock.json. Run npm install to regenerate the lockfile, then commit both files.
- What causes "npm ERR! code EUSAGE npm ERR! `npm ci` can only install packages when"?
- package.json edited manually without running npm install Dependency version changed in package.json but lockfile not updated package-lock.json not committed after adding new dependency
- Is the "npm ERR! code EUSAGE npm ERR! `npm ci` can only install packages when" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐npm Official Documentation
- ๐npm ci Documentation(Official Docs)
- ๐ฌStack Overflow: npm(Stack Overflow)
- ๐GitHub: npm(GitHub)