/

How to Remove All the node_modules Folders Content

How to Remove All the node_modules Folders Content

Save Disk Space by Deleting Unnecessary node_modules Folders

Recently, I needed to transfer a folder filled with old projects to a new computer. When I tried compressing it, I was surprised to find that its size was a whopping 8GB. Quite excessive for simple coding projects that mainly consist of text files.

Since all the projects were written in JavaScript, each one had a node_modules folder. However, these folders are completely unnecessary because they can be regeneratd by running npm install within each project. Moreover, most of the projects in that folder were archaic and would never be used again.

To free up space, I decided to remove all the node_modules folders. However, there were too many to individually delete. So, I navigated to the parent folder, which I named dev, and executed the following command in the ZSH terminal (the default shell in macOS Catalina):

1
find . -name "node\_modules" -type d -prune -exec rm -rf '{}' +

This single line command efficiently reduced the folder’s size from 8GB to 2GB. A significant improvement!

I discovered this command on a helpful blog post here. If you require a more detailed explanation of the command, please refer to the blog.

tags: [“node_modules”, “disk space”, “JavaScript”, “npm install”, “ZSH terminal”, “folder size”, “coding projects”]