Removing node_modules recursively

11/08/2019 — 1 Min Read — In Tips

When you use npm packages, they're saved into node_modules directory. You end up with a huge directory containing all the dependencies which may contain thousands of files. What if you work on multiple projects? That's an even bigger mess. Removing node_modules from each project takes a lot of time.

How to get rid of them all? Open a console and go to your projects folder. Then type:

find . -name 'node_modules' -type d -prune

It will list all folders to be removed. Then to do actual deletion:

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +

You can use it for any rubbish🗑️ directory too, like .cache.

Something easier?

Check out npkill package. It simply searches your drive and shows you a neat list. Then just click space to remove selected directories. It's just:

npm install -g npkill
npkill

See the demo below (source: package repository):

Demonstration of npkill usage