← npm Lockfile Duplicates Check

How to remove duplicate packages from node_modules

npm installs one copy of a package when every dependant accepts the same version. When two dependants need versions that cannot be the same, it nests a second copy under one of them. A large app can end up with dozens of these.

The two kinds this tool reports

Checklist

  1. Run the check on package-lock.json, once with devDependencies and once with --no-dev: only what you ship affects users.
  2. Run npm dedupe, then the check again. Same-major entries should shrink.
  3. For a cross-major copy, look at who requires the old version (the report names the dependant) and check whether a newer release of that dependant moves to the new major.
  4. Use overrides only when you can test the result; forcing a version a dependant was not built for can break it silently.
  5. Add node dupes-check.mjs --max-same-major 0 to CI so dedupable copies cannot creep back.

What this does not tell you

A lockfile records versions, not sizes, so the count of extra copies is not a byte count. Some duplicates are harmless and some, such as two copies of a library with global state, matter a lot. The list is for review; it is not a finding that anything is broken.