Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find npm duplicate packages?

npm dedupe can flatten the folder structure. However, before doing that. I hope to see a list of duplicate packages, so that I know whether or not to go forward. Is there such a feature? If not, is there some sort of scripts that help me achieve this?

like image 753
jackyzhai Avatar asked Feb 02 '16 03:02

jackyzhai


People also ask

What is npm Dedupe?

deduped is short for "deduplicated" (duplicates were removed). The documentation for npm dedupe explains how npm does this: Searches the local package tree and attempts to simplify the overall structure by moving dependencies further up the tree, where they can be more effectively shared by multiple dependent packages.

What does npm prune do?

Description. This command removes "extraneous" packages. If a package name is provided, then only packages matching one of the supplied names are removed. Extraneous packages are those present in the node_modules folder that are not listed as any package's dependency list.

Does npm install multiple versions of same package?

With npm or yarn, you can install a package under a custom alias. This enables you to install multiple versions of a package in the same project. Read the documentation on aliasing with npm here and yarn here.

How do I see all npm installs?

To check for all globally installed packages and its dependencies, run the npm list command followed by the -g flag. This above command prints the all globally installed packages in tree view.


2 Answers

Try this:

 npm ls --parseable | xargs -L1 sh -c 'basename $1' dummy | sort | uniq -c | grep -v "^ *1 " | sort -rn

The pipeline here is:

  1. List packages in parsable format
  2. Strip each path down to only the package name
  3. Sort package names alphabetically to prepare for counting unique names
  4. Group and count unique package names
  5. Hide packages which are not duplicated (count = 1)
  6. Sort again by descending number of occurrences
like image 117
mzulch Avatar answered Oct 21 '22 17:10

mzulch


Try find-duplicate-dependencies. As well as versions it shows you the simplified path to the duplicates:

$ find-duplicate-dependencies
This package has the following duplicate dependencies:

regenerator-runtime :
[ { name: 'regenerator-runtime',
    version: '0.11.1',
    from: undefined,
    path: 'project/react-select/emotion/babel-plugin-emotion/babel-core/babel-runtime' },
  { name: 'regenerator-runtime',
    version: '0.12.1',
    from: undefined,
    path: 'project/recompose/@babel/runtime' } ]
like image 26
Tamlyn Avatar answered Oct 21 '22 16:10

Tamlyn