Avatar of Andrea Pavone
Andrea Pavone

Optimizing Disk Space and Backup Efficiency by Managing Project Dependencies

Working on multiple projects can quickly lead to a cluttered and space-consuming development environment. Two common culprits for this issue are the node_modules directory in JavaScript/Node.js projects and the vendor directory in PHP projects managed by Composer. These directories store project dependencies and can grow significantly in size, especially if you're working on numerous repositories.

The Problem with node_modules and vendor Directories

node_modules

In JavaScript and Node.js projects, the node_modules directory is created when you run npm install or yarn install. This directory contains all the dependencies required for your project to run. While essential for development, these directories can consume a substantial amount of disk space, particularly if you have multiple projects or versions of dependencies.

vendor

Similarly, in PHP projects managed by Composer, the vendor directory is created when you run composer install. This directory contains all the PHP libraries and dependencies your project needs. Like node_modules, the vendor directory can become quite large, especially in projects with many dependencies.

Finding and Removing Unnecessary Dependencies

To manage disk space more effectively, you can use the following commands to find and remove these directories.

Finding and Removing node_modules Directories

To find all node_modules directories within a project directory and calculate their sizes, you can use the following command:

find `pwd` -type d -maxdepth 3 -name 'node_modules' | xargs -n 1 du -sh

This command will list all node_modules directories up to three levels deep from the current directory (`pwd`) and display their sizes.

To remove these directories and free up space, you can use:

find `pwd` -type d -maxdepth 3 -name 'node_modules' | xargs -n 1 rm -fr

Finding and Removing vendor Directories

Similarly, to find and calculate the sizes of vendor directories, use:

find `pwd` -type d -maxdepth 3 -name 'vendor' | xargs -n 1 du -sh

And to remove them:

find `pwd` -type d -maxdepth 3 -name 'vendor' | xargs -n 1 rm -fr

Optimizing Time Machine Backups

In addition to freeing up disk space, you can optimize your Time Machine backups by excluding these directories. This will make your backups more efficient and faster.

To exclude node_modules directories from Time Machine backups, use:

find `pwd` -type d -maxdepth 3 -name 'node_modules' | xargs -n 1 tmutil addexclusion

And to exclude vendor directories:

find `pwd` -type d -maxdepth 3 -name 'vendor' | xargs -n 1 tmutil addexclusion