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 npkillWhen 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 ~/projectsShow node_modules while excluding anything under 50MB
npx npkill --size-greater-than 50Show only paths that contain a given word (for example react)
npx npkill --filter reactDelete everything with no confirmation dialog (extremely dangerous, at your own risk!)
npx npkill --no-interactive --delete-allShow the help (see all the options)
npx npkill --helpSummary
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.
| Situation | What to do |
|---|---|
Want to check scattered node_modules all at once | Run npx npkill to list them |
| Want to pick and delete them individually | Select with the arrow keys and delete with space/Enter |