LinuxCommandLibrary

npm-dedupe

Reduce duplication of packages in node_modules

TLDR

Deduplicate packages in node_modules

$ npm dedupe
copy

Follow package-lock.json or npm-shrinkwrap.json during deduplication
$ npm dedupe --lock
copy

Run deduplication in strict mode
$ npm dedupe --strict
copy

Skip optional/peer dependencies during deduplication
$ npm dedupe --omit [optional|peer]
copy

Enable detailed logging for troubleshooting
$ npm dedupe --loglevel verbose
copy

Limit deduplication to a specific package
$ npm dedupe [package_name]
copy

SYNOPSIS

npm dedupe [package-spec]

PARAMETERS

package-spec
    A specific package or packages to deduplicate. If omitted, dedupe operates on the entire project.

DESCRIPTION

The `npm dedupe` command analyzes your project's `node_modules` directory and attempts to simplify the dependency tree by moving common dependencies higher up. This reduces redundancy and can improve installation time, disk space usage, and overall project performance. It traverses the dependency graph, identifies duplicate packages with the same version, and modifies the `package.json` and `package-lock.json` (or `npm-shrinkwrap.json`) files to reflect the new, more efficient structure.
Deduping is particularly useful in large projects with complex dependency trees, where multiple dependencies might rely on the same underlying package. It helps ensure that only one copy of each package is installed at the highest possible level in the `node_modules` hierarchy, preventing version conflicts and reducing the overall size of the project.
It's important to note that `npm dedupe` only works within the constraints of your project's declared dependencies and version ranges. It won't automatically update dependencies to the latest versions, but it will ensure that the most efficient use is made of the versions already specified.

CAVEATS

Deduplication may not be possible if packages have conflicting version ranges or if the dependency tree is structurally complex. Also, may break due to hoisting limitations

HOW IT WORKS

The command analyzes the `node_modules` directory and checks if multiple versions of the same package are installed at different levels. If it finds duplicate packages with compatible versions, it moves one instance of the package higher up the dependency tree, making it a shared dependency for all packages that require it. It then updates the `package-lock.json` or `npm-shrinkwrap.json` file to reflect the changes in the dependency tree.

USAGE SCENARIOS

Use `npm dedupe` when you notice your project has a large `node_modules` directory, long installation times, or potential version conflicts. Running it periodically can help maintain a clean and efficient dependency tree.

SEE ALSO

npm install(1), npm update(1), npm shrinkwrap(1)

Copied to clipboard