javascriptnode.jsnpm

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?


Solution

  • 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