Save disk space by deleting unused node_modules

believer

Rickard Natt och Dag

Posted on November 21, 2021

Save disk space by deleting unused node_modules

I quickly understood that I had forgotten to remove node_modules when I wanted to copy my projects folder to a new computer and the process was going to take multiple hours. Luckily, my dotfiles already contained a command to remove all the node_modules folders at once. This time the command saved me ~40 GB.

# Use the command at you own risk
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +
Enter fullscreen mode Exit fullscreen mode

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 matches node_modules
  • -type d - We are looking for a directory (d)
  • -prune - Stops find from descending into the folder, meaning that it won't look for node_modules inside node_modules and so on.
  • -exec rm -rf '{}' + - Runs the specified command, rm, with flags r (remove directory) and f (do not ask for confirmation no matter what the file permissions are). '{}' will be replaced by the pathname that's been found. + means that find will append all the file paths to a single command instead of running rm for each.

If you just 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 -chs
Enter fullscreen mode Exit fullscreen mode

There's also an npm command that you can use by running npx npkill if you don't want to mess with terminal commands.

πŸ’– πŸ’ͺ πŸ™… 🚩
believer
Rickard Natt och Dag

Posted on November 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related