Find unused npm packages in package.json

181,478

Solution 1

You can use an npm module called depcheck (requires at least version 10 of Node).

  1. Install the module:

     npm install depcheck -g
     or
     yarn global add depcheck
    
  2. Run it and find the unused dependencies:

     depcheck
    

The good thing about this approach is that you don't have to remember the find or grep command.

To run without installing use npx:

npx depcheck 

Solution 2

There is also a package called npm-check:

npm-check

Check for outdated, incorrect, and unused dependencies.

enter image description here

It is quite powerful and actively developed. One of it's features it checking for unused dependencies - for this part it uses the depcheck module mentioned in the other answer.

Solution 3

Check the unused dependencies

npm install depcheck -g
depcheck

enter image description here

Check the outdated library

npm outdated

enter image description here

Solution 4

The script from gombosg is much better then npm-check.
I have modified a little bit, so devdependencies in node_modules will also be found.
example sass never used, but needed in sass-loader

#!/bin/bash
DIRNAME=${1:-.}
cd $DIRNAME
FILES=$(mktemp)
PACKAGES=$(mktemp)
# use fd
# https://github.com/sharkdp/fd
function check {
    cat package.json \
        | jq "{} + .$1 | keys" \
        | sed -n 's/.*"\(.*\)".*/\1/p' > $PACKAGES
    echo "--------------------------"
    echo "Checking $1..."
    fd '(js|ts|json)$' -t f > $FILES
    while read PACKAGE
    do
        if [ -d "node_modules/${PACKAGE}" ]; then
            fd  -t f '(js|ts|json)$' node_modules/${PACKAGE} >> $FILES
        fi
        RES=$(cat $FILES | xargs -I {} egrep -i "(import|require|loader|plugins|${PACKAGE}).*['\"](${PACKAGE}|.?\d+)[\"']" '{}' | wc -l)
        if [ $RES = 0 ]
        then
            echo -e "UNUSED\t\t $PACKAGE"
        else
            echo -e "USED ($RES)\t $PACKAGE"
        fi
    done < $PACKAGES
}
check "dependencies"
check "devDependencies"
check "peerDependencies"

Result with original script:

--------------------------
Checking dependencies...
UNUSED           jquery
--------------------------
Checking devDependencies...
UNUSED           @types/jquery
UNUSED           @types/jqueryui
USED (1)         autoprefixer
USED (1)         awesome-typescript-loader
USED (1)         cache-loader
USED (1)         css-loader
USED (1)         d3
USED (1)         mini-css-extract-plugin
USED (1)         postcss-loader
UNUSED           sass
USED (1)         sass-loader
USED (1)         terser-webpack-plugin
UNUSED           typescript
UNUSED           webpack
UNUSED           webpack-cli
USED (1)         webpack-fix-style-only-entries

and the modified:

Checking dependencies...
USED (5)         jquery
--------------------------
Checking devDependencies...
UNUSED           @types/jquery
UNUSED           @types/jqueryui
USED (1)         autoprefixer
USED (1)         awesome-typescript-loader
USED (1)         cache-loader
USED (1)         css-loader
USED (2)         d3
USED (1)         mini-css-extract-plugin
USED (1)         postcss-loader
USED (3)         sass
USED (1)         sass-loader
USED (1)         terser-webpack-plugin
USED (16)        typescript
USED (16)        webpack
USED (2)         webpack-cli
USED (2)         webpack-fix-style-only-entries

Solution 5

many of the answer here are how to find unused items.

what if.. I wanted to..

AUTOmatically -- a) find + b) Remove the unused items


Option 1:

  1. Install this node project.
 $ npm install -g typescript tslint tslint-etc

  1. At the root dir, add a new file tslint-imports.json
{
  "extends": [
    "tslint-etc"
  ],
  "rules": {
    "no-unused-declaration": true
  }
}

  1. Run this at your own risk, make a backup :)
$ tslint --config tslint-imports.json --fix --project .

Option 2: Per @Alex

npx depcheck --json | jq '.dependencies[]' | xargs -L1 npm rm
Share:
181,478
Josh C
Author by

Josh C

Updated on February 05, 2022

Comments

  • Josh C
    Josh C 11 months

    Is there a way to determine if you have packages in your package.json file that are no longer needed?

    For instance, when trying out a package and later commenting or deleting code, but forgetting to uninstall it, I end up with a couple packages that could be deleted.

    What would be an efficient way to determine if a package could safely be deleted?

  • Boern
    Boern about 7 years
    return new Promise(function (resolve, reject) {: ReferenceError: Promise is not defined ... too bad. gonna go with npm-check
  • cyberwombat
    cyberwombat almost 7 years
    depcheck-es6 is now merged into depcheck
  • phil294
    phil294 almost 6 years
    doesnt look useful. I am using the standard angular2 cli setup and depcheck lists every package as unused which is just wrong
  • Westy92
    Westy92 almost 6 years
    I used this over npm-check because there's a module for gulp integration: github.com/depcheck/gulp-depcheck.
  • lapint over 4 years
    This package completely lagged down my Mac running High Sierra, I would not recommend using it.
  • Sakshi Nagpal over 4 years
    It shows some dependencies as unused which are actually getting used such as babel-cli, css-loader, sass-loader and many more which are getting used in build process.
  • Doug Domeny
    Doug Domeny over 4 years
    For typescript, install using npm install -g depcheck typescript. However, even with this option, every dependency was listed as unused.
  • Alex K
    Alex K over 4 years
    Seems to give me the same results as depcheck. It looks like it even uses depcheck to find the unused dependencies.
  • Javier Arias
    Javier Arias about 4 years
    NB. depcheck doesn't take into account packages used in scripts specified in package.json
  • mgarde
    mgarde about 4 years
    npm outdatedchecks and lists current, wanted and latest package versions. No list of unused packages though.
  • Kiril about 4 years
    To run it just once (w/o installation) - use npx: npx depcheck
  • swateek
    swateek about 4 years
    I had a grunt build, and this suggested to remove all grunt packages under devDependencies. Not a good idea :)
  • German Attanasio
    German Attanasio about 4 years
    There is an option to ignore certain packages like eslint-* or gulp-.
  • sanjeev almost 4 years
    so now who will remove this depcheck?
  • Atte Juvonen
    Atte Juvonen almost 4 years
    Doesn't work. I just ran depcheck on my Gatsby project and it listed almost all of my packages as unused even though most of them are actually used.
  • German Attanasio
    German Attanasio almost 4 years
    @AtteJuvonen there is a way to specify which pages should be skipped like eslint or babel. What are the packages that are being listed for you?
  • Atte Juvonen
    Atte Juvonen almost 4 years
    It lists a lot of packages that are actually used, including gatsby-plugin-google-analytics and typescript.
  • dev27 over 3 years
    Didn't work for me. It listed all the packages as unused.
  • Kyle Burkett
    Kyle Burkett over 3 years
    doesnt look useful as well. I am using the standard angular setup and this also lists every package as unused which is just as wrong
  • OZZIE
    OZZIE over 3 years
    it revealed some not used ones but also used ones, still helpful I guess :-) It doesn't understand webpack loaders ;-)
  • OZZIE
    OZZIE about 3 years
    What about .jsx files and .ts files etc :D
  • OZZIE
    OZZIE about 3 years
    Apparently using this approach we are not using react module in our React app :D
  • angularrocks.com almost 3 years
    Running on old angular project saying No depcheck issue which is wrong as there is definitely some unused packages I can tell
  • vdiaz1130
    vdiaz1130 almost 3 years
    Editors sometimes cause imports to wrap into multiple lines. Would this script catch statements where ‘import’ or ‘require’ would be on a different line than the ‘from “PACKAGE_NAME”’? In other words, does it ignore whitespace in import or require statements?
  • Ayon Nahiyan over 2 years
    But this is going to remove from the js files only. But ya still good.
  • alex
    alex over 2 years
    how about npx depcheck --json | jq '.dependencies[]' | xargs -L1 npm rm
  • el-davo over 1 year
    Yeah same as the rest, finding depcheck is pretty useless to be honest, lists many packages that are in use as unused.
  • jjmerelo
    jjmerelo over 1 year
    Seems a bit outdated now. It includes high severity vulnerabilities right now...
  • Justin Dehorty
    Justin Dehorty over 1 year
    Adding -P 32 switch to your xargs will result in a huge speedup.
  • Cameron Wilby
    Cameron Wilby over 1 year
    Best solution compared to depcheck and derivatives. Adding --max-procs|-P 32 greatly improves the speed.
  • JCollier
    JCollier about 1 year
    depcheck jacked up my dependencies pretty badly. I just blindly trusted it and removed the packages that it said were unused. My project still built, but there were unseen problems with removing the dependencies, and down the road, I had a lot of issues.
  • Manwe
    Manwe about 1 year
    Great script that nicely extended the orginal one, but it got unusable slow (even xargs -P options) on a large react app. Re-organized file searches and shared a version that should produce same output, but not necessary in the same order.
  • AndyW
    AndyW 12 months
    tslint is deprecated as of 2019
  • daveD
    daveD 11 months
    depcheck didnt work for me. It listed some as unused, so I removed them. Then when I did a build, it failed to find the dependencies I just removed !!