Skip to content

Instantly share code, notes, and snippets.

@padcom
Last active September 20, 2022 23:54
Show Gist options
  • Save padcom/6ee9c93dadd77ede38ba54ca6e76309c to your computer and use it in GitHub Desktop.
Save padcom/6ee9c93dadd77ede38ba54ca6e76309c to your computer and use it in GitHub Desktop.
A command that deletes all node_modules folders recursively.

Cleaning up node_modules

Working with node.js is great. NPM may not be the best package manager on the planet, but it is good enough. However, if you have a number of projects on the disk then the copies of modules keep piling up...

This command does the cleaning. It's interactive!

#!/bin/bash
printf "Calculating..."
folders=$(find . -name 'node_modules' -type d -prune)
printf "Done.\n\n"
echo "The following folders will be deleted:"
echo "$folders"
printf "\n\n"
read -p "Are you sure you wish to continue? (type 'yes' and press enter) >"
if [ "$REPLY" != "yes" ]; then
echo "Aborted."
exit
fi
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment