[HOW-TO] Remove all node_modules folders 🗑️

Published by Alicia's Notes 🚀, View original

Node Modules killing your hard drive space? Manually finding and removing is a pain, so here are some simple solutions

Option #1 - Natively

From your desired entry point, all you need to do, is run:

$ find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;

⚠️ This will delete all nested node_modules directories, and their contents ⚠️

If you'd first like to list of all node_modules within your current directory, you can run:

$ find . -name "node_modules" -type d -prune -print | xargs du -chs

This'll output the path, and total size of each occurrence of node_modules, and finish of by showing to cumulative total size

Option #2 - NPKill

NPKill is a simple, yet effective package that finds and removes all node_modules folders within your system.

After installing (with npm i -g npkill), just run npkill to start the CLI. From there you will be able to see all node_modules directories, along with their size and other meta data. You can then choose to delete them one-by-one, or all instances within certain parameters.

Gif: Searching for all node_modules Gif: Deleting selected, or all node_modules

Note for Windows users

The above options are for Unix systems (Linux/ MacOS), if you need to do this natively, try the following commands, (thanks to Sahil Malik).

View which folders will be deleted:

FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"

Initiate the Extermination:

FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"

Done 😀