A Developer’s Guide to Efficiently Removing node_modules

As a developer, it’s common to juggle multiple projects simultaneously. This often leads to the accumulation of numerous node_modules folders, which can consume significant storage space on your Mac. Over time, this not only clutters your system but may also cause storage-related notifications about low space. In this post, we will see how to remove all node modules in subdirectories and directories using command line.

Understanding the Impact of Node_Modules

Node_Modules directories are created by Node.js when installing packages and dependencies for a project. While essential during development, these folders can become redundant and storage-heavy once a project is completed or put on hold.

Step-by-Step Guide to Removing Node_Modules

Locating the Node_Modules:

  • Begin by changing the directory to the location you want to check for node_modules. For instance, if you want to check the Documents directory, use the command $ cd documents.

Identifying the Size and Location of Node_Modules:

  • To find all node_modules directories and display their locations along with their size, use the command:
$ find . -name "node_modules" -type d -prune -print | xargs du -chs

Reviewing the Output:

  • The output of this command will list the size and path of each node_modules folder, helping you understand the space they occupy.

Deleting the Node_Modules:

  • To remove all node_modules directories displayed in the output, use the command:
$ find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;

This guide simplifies the process of managing storage space on your Mac, specifically targeting the often overlooked node_modules folders. By following these steps, you can keep your system organized and free up valuable space.