til / save disk space by deleting unused node_modules
If you're like me and have a bunch of side projects, chances are that you also have a ton of node_modules folders taking up unnecessary space. I'm going to show you a command to delete all of them at once. Last time I used it I saved ~40 GB. I would recommend deleting all of them and then reinstalling in the projects you're actively developing. Start off with a clean slate.
# Use the command at you own risk
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +There's a lot to the command, but here's an explanation of each part to
demystify it.
find- A command that comes built-in with MacOS and Linux..- Look from this location-name "node_modules"- Make sure the last component of the pathname matchesnode_modules-type d- We are looking for a directory (d)-prune- Stopsfindfrom descending into the folder, meaning that it won't
look fornode_modulesinsidenode_modulesand so on.-exec rm -rf '{}' +- Runs the specified command,rm, with flagsr(remove directory) andf(do not ask for confirmation no matter what the file permissions are).'{}'will be replaced by the pathname that's been found.+means thatfindwill append all the file paths to a single command instead of runningrmfor each.
If you only want to find node_modules folders and display their disk size use
the following command.
find . -name "node_modules" -type d -prune -print | xargs du -chsThere's also an
npmcommand that you can use by runningnpx npkillif you
don't want to mess with terminal commands.