Deleting Scattered node_modules in Bulk with npkill

Clean up scattered node_modules folders with npkill

* This page contains promotional content

When you work with npm / yarn / pnpm a lot, node_modules directories get created one after another, and before you know it they are eating into your disk space.
Repositories I tried out, verification projects I left lying around, old clones… hunting down the scattered node_modules one by one and running rm -rf is tedious, so I am noting down that a command called npkill makes tidying up easy.

What npkill is

It is a CLI tool you can use just by running npx npkill; it searches the disk for installed node_modules folders and lets you pick and delete them interactively.

npx npkill

When you run it, everything under the current directory is scanned and the node_modules it finds are listed. Move the cursor with the arrow keys, pick the folder you want to delete, and press the space key (or Enter), and it is deleted on the spot.

Frequently used npkill examples

Search only a specific path (for example ~/projects)

npx npkill -d ~/projects

Show node_modules while excluding anything under 50MB

npx npkill --size-greater-than 50

Show only paths that contain a given word (for example react)

npx npkill --filter react

Delete everything with no confirmation dialog (extremely dangerous, at your own risk!)

npx npkill --no-interactive --delete-all

Show the help (see all the options)

npx npkill --help

Summary

Picking and deleting interactively with npx npkill is faster than manually running find on scattered node_modules and then rm -rf.
It needs no installation and can be run from npx, so it is also nice that you can use it the moment you think of it.

SituationWhat to do
Want to check scattered node_modules all at onceRun npx npkill to list them
Want to pick and delete them individuallySelect with the arrow keys and delete with space/Enter

References

oss 
npm