npm dedupe: Flatten Duplicate Deps
npm dedupe reduces duplicate packages by hoisting shared versions.
Duplicate copies of a library cause subtle bugs (two React instances, failed instanceof checks). dedupe collapses them where versions allow.
Common usage
npm dedupe- flatten the tree--dry-run- previewnpm ls <pkg>- confirm duplicates first
Example in CI
Check then dedupe.
shell
npm dedupe --dry-runIn CI
Run dedupe locally and commit the updated lockfile; do not dedupe in the build. It only helps when versions are compatible.
Key takeaways
- dedupe collapses compatible duplicates.
- Commit the resulting lockfile.
- Use
npm lsto find duplicates.