til / delete unused node modules
After developing on a computer for a while you'll probably end up with a bunch of projects. If those projects are JavaScript there's a good change that they contain a node_modules directory. From time to time it's a good idea to remove all of these folders, since they can get quite big, and re-download the dependencies in the projects you're actively using.
I have aliased the following command to node-prune and have been using it a couple of years without any issues. When I last ran the command on I got back ~40 GB of disk space. Use it at your own risk.
alias node-prune='find . -name "node_modules" -type d -prune -exec rm -rf '{}' +'If you only want to find and display the size of the folders you can use the following command
find . -name "node_modules" -type d -prune -print | xargs du -chsThere's also npkill which looks up and displays node_modules, displays their size and allows you to delete the folders. Run it by using npx npkill.
I also posted this on my blog with explanations of the command.
- Erik Rasmussen. (2021-11-09). Tweet