Skip to content
jshacks.dev
Packages

Find duplicate packages in a lockfile

List every copy of a package in your tree with npm ls --all --parseable to find duplicates, then try npm dedupe. Applies to npm 10 and its lockfile.

1 min read
Summarize in AI

A bundle is larger than it should be, and you suspect two versions of one library. npm ls with --all prints every copy, and --parseable prints plain paths you can read at a glance.

Shell
npm ls ms --all --parseable
Text
/home/dev/app
/home/dev/app/node_modules/ms
/home/dev/app/node_modules/legacy-lib/node_modules/ms

Two paths for ms mean two installed copies: one at the top and one nested under legacy-lib. The first line is the project itself. The flags are listed in the npm ls docs.

Next, see whether npm can merge them:

Shell
npm dedupe --dry-run

npm dedupe collapses copies only when one version satisfies every range that asks for it. When ranges conflict, as with legacy-lib pinning an older ms, the duplicate stays until you upgrade or override that package.

Search the lockfile too: package-lock.json lists each nested copy under its own node_modules/... key. Bundlers can still end up with two copies when the same code arrives through different paths, so check the build output when size matters.

Taggednpm
Share

More snippets

Packages

Initialize a package with npm init

Initialize a package.json with npm init, skip the questions with the yes flag, and set your own defaults for author and license in npm config.

1 min read
Packages

Generate a changelog from commits

Turn conventional commit messages into a CHANGELOG.md section with conventional-changelog, grouped by feature and fix. Works with any Git history.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.