DEV Community

Cover image for Remove unused npm modules in less than 30 seconds!
Manitej ⚡
Manitej ⚡

Posted on

Remove unused npm modules in less than 30 seconds!

In this quick tutorial, I'll tell you how you can find the unused npm modules in your project and remove them.

Go to the project's root folder and run the below command,

npx depcheck
Enter fullscreen mode Exit fullscreen mode

It will display all the unused packages in your project.

To uninstall a module simply run the below command

npm uninstall <package>
Enter fullscreen mode Exit fullscreen mode

or

yarn remove <package>
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
guhkun13 profile image
name cannot be blank • Edited

so i created this script to remove ALL UNUSED deps, (works in unix):

#!/bin/bash
file='unused-deps.txt'
echo "list all unused deps"
npx depcheck > $file
n=1
while read line; do
dep=$( echo "$line" | cut -c 3- )
echo "uninstall : $dep"
npm uninstall "$dep"
n=$((n+1))
done < $file

  • save the script as whatever you want, ex: removeUnusedDeps.sh
  • then bash removeUnusedDeps.sh in your terminal
  • Collapse
     
    bravemobin profile image
    Bravemobin

    nice idea !

    Collapse
     
    richtone profile image
    Richard Turza

    I love you!

    Collapse
     
    coolaj86 profile image
    AJ ONeal (formerly @solderjs) • Edited

    Uninstall all of the unused deps:

    npm install -g depcheck
    
    npx depcheck | sed '/Missing/q' | grep '\*' | cut -c 3- | while read my_package; do
      npm uninstall "$my_package"
    done
    
    Enter fullscreen mode Exit fullscreen mode

    Note: You may want to run npx depcheck again right after, as you may have removed something that you didn't use, but it may have had a dependency that you did use.

    Collapse
     
    dreamhigh915 profile image
    DreamHigh

    Hi.
    Do you know how to get the list of all the unused modules and uninstall them automatically?

    Thank you.