How to Delete "node_modules" In a Style

How to Delete "node_modules" In a Style

For Javascript projects, the node_modules folder is used to save all downloaded packages from npm locally in your computer. JS developers often complain that their hard drive is mysteriously always full.

StorageFull.png

Every time you start a new (side) project and enter npm install it downloads a bunch of JS files into the node_modules folder that you totally forget about after you abandon this project.

npminstall.jpg

You could go to every node_modules folder and click "Right Click" then "Delete" or you can use the following command for Mac / Linux OS:

rm -rf node_modules

The command above will first delete the content of node_modules recursively until all of it is deleted, then it will remove the node_modules folder too.

If you are a senior developer, you might create a bash script to find all the node_modules folder and delete them as follow:

find . -name "node_modules" -type d -prune -exec rm -rf '{}' +

But here is how you can delete your node_modules folder in a style which I find it the smartest way to get the job done is to run:

npx npkill

It will traverse your file system and find all the node_modules folders that can be removed, give you a breakdown of their sizes and allow you to delete them one by one by hitting the space bar.

NpxNpkill.png

That's it!

For more details, check out npkill package.

I'd love for you to leave me a feedback below in the comments!

Did you find this article valuable?

Support Basim Hennawi by becoming a sponsor. Any amount is appreciated!