To find node_modules on a Mac, list them first with find in your projects folder and -prune, check sizes with du, then delete the ones in finished projects. Each rebuilds with npm install, yarn or pnpm install. Then clear package manager caches separately.
Why does node_modules use so much space?
Every JavaScript project installs its own copy of its dependencies, and each dependency brings its own. A single project's node_modules is often hundreds of megabytes, and a developer with dozens of old projects can have tens of gigabytes spread across a disk. Because the folder is generated from package.json and the lockfile, it is among the safest large things to remove from a project you are not working on.
How do I list every node_modules folder first?
List before you delete. This command searches a folder you name, prints each node_modules it finds, and does not descend into them (that is what -prune does, and it also keeps nested copies from being listed over and over):
find ~/Projects -name node_modules -type d -prune -print
Change ~/Projects to where you keep code. To see sizes, largest last:
find ~/Projects -name node_modules -type d -prune -exec du -sh {} + 2>/dev/null | sort -h
Read the list. Each line is a project you either still use or don't. Delete one by hand if you want to be careful:
rm -rf ~/Projects/old-app/node_modules
How do I delete all of them at once?
Only after you have read the list. The same find with an action added:
find ~/Projects -name node_modules -type d -prune -exec rm -rf {} +
This permanently deletes every node_modules folder under that path, skipping the Trash, and cannot be undone. Never point it at your home folder or at /, where it would hit folders inside apps and tools. Make sure no dev server, build or editor task is running in those projects.
Exclude a project you are using by running it per folder, or just delete the folders for finished projects with the one-by-one command above. To get a project back: npm ci (installs exactly what the lockfile says), yarn install or pnpm install. You need a network connection unless the package manager's cache already has everything.
A few cases to check before deleting: a package you patched by editing files inside node_modules, a project whose lockfile is missing, and work you must do offline. Detail on the folder is at node_modules folders.
How do I clear the npm, Yarn and pnpm caches?
These are separate from node_modules. They hold downloaded package files so reinstalls are fast. Each can be deleted and is refilled on demand.
| Tool | Find the folder | Clear it |
|---|---|---|
| npm | npm config get cache (default ~/.npm) | npm cache verify, or npm cache clean --force |
| Yarn (classic) | yarn cache dir | yarn cache clean |
| pnpm | pnpm store path | pnpm store prune |
npm's documentation says the cache is self-healing, that npm cache clean requires --force, and that you normally only need it to reclaim disk space. npm cache verify checks the cache and removes unneeded data, so try it first. pnpm store prune removes packages that no project uses anymore; pnpm's docs call it harmless but suggest not running it too often, because switching branches may need packages again. Details: npm cache, Yarn cache and pnpm store.
npm cache clean --forceandyarn cache cleandelete the cache outright. The next install downloads packages again. Nothing in your projects changes.
If you use pnpm, note that its node_modules folders are built from links into the shared store. Deleting them frees less than du reports, and the store is where the real files live. Newer Yarn versions (Berry) may keep a cache inside each project instead, so check yarn cache dir before assuming a location.
How do I stop node_modules from filling the disk again?
- Delete
node_moduleswhen you finish with a project, and keep the lockfile in git. - Prefer pnpm for many projects, since it stores each package version once.
- Run the size listing above every few months.
How does Clean House handle node_modules?
Clean House recognizes node_modules folders as developer data and lists them with their size, the project they belong to and when that project was last opened. The "Why?" card says what happens if one goes: it comes back with an install. Each gets a call: Safe to clear, Look first or Keep. It avoids a project you are using right now. Cleaning moves items to the Trash after a re-check, and Recently Cleared can put them back, so you don't need to write a find command or worry about the wrong path.