Skip to content
Latchkey

npm dedupe: Usage & Common Errors

Collapse duplicate dependencies into a flatter tree.

npm dedupe (alias npm ddp) reorganizes node_modules so packages that can share a single compatible version do, shrinking the tree and avoiding duplicate-instance bugs.

What it does

Walks the dependency tree and hoists packages to the highest position where one version satisfies all dependents, removing redundant nested copies. It updates package-lock.json to match. It cannot merge versions that are genuinely incompatible.

Common usage

Terminal
npm dedupe                  # flatten the tree
npm dedupe --dry-run        # preview the changes
npm find-dupes              # report duplicates without changing

Common CI gotcha: lockfile changes after dedupe

Running npm dedupe in CI rewrites the lockfile and then conflicts with npm ci. Run it locally, commit the resulting package-lock.json, and keep CI strict - dedupe is a maintenance step, not a build step.

Terminal
# locally:
npm dedupe && git add package-lock.json && git commit -m "dedupe deps"

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →