Somewhere on your Mac there are dozens of node_modules folders, and you haven't thought about most of them in months. Each one is 200 MB–2 GB of installed packages — including the ones for projects you deleted, archived, or rage-quit two laptops ago.
This is the single biggest "quiet" disk eater for anyone who's ever run npm install. Let's find them all, safely delete the dead ones, and stop them from re-growing.
First, measure: how bad is it?
find . -name "node_modules" -type d -prune 2>/dev/null | head -50
That lists the folders. For the actual byte counts (the number that matters), do this from a big directory like ~/Developer, ~/Projects, or your home folder:
find . -name "node_modules" -type d -prune -exec du -sh {} + 2>/dev/null | sort -rh | head -20
You'll see the map: a handful of heavy projects, a graveyard of old ones. A typical developer who's shipped a few projects has 10–50 GB tied up here.
What's safe to delete?
Everything under node_modules is disposable. That's the whole point — it's re-downloadable packages, not your code. Your actual work lives in the project's src/, package.json, package-lock.json / yarn.lock / pnpm-lock.yaml.
Rules of thumb:
-
Delete the whole folder when a project is archived on GitHub/GitLab —
rm -rf project/node_modulesreclaims everything. - Only reinstall if you need it — you'll swap it in from git before you work on it again.
-
Never delete a project folder believing its
node_modulesis your work — your code is separate.
For an individual project:
cd ~/Projects/old-thing
rm -rf node_modules
npm ci
That drops it to the locked package set — and if the lockfile is gone, npm install quietly does the same.
The recursive one-liner (delete nothing blindly)
If you want to strip node_modules from a whole tree of projects in-place (not delete the projects), this is the surgical version:
find . -name "node_modules" -type d -prune -ok rm -rf {} \; 2>/dev/null
The -ok asks before every deletion — boring but safe. Drop -ok and swap to -exec rm -rf {} + only when you've visually inspected the du -sh list above and know exactly what you're targeting.
Also on the Node disk-bloat list
node_modules isn't alone. The other Node space-suckers:
-
~/.npm/_cacache— npm's global cache. Safe to clear. -
~/.yarn/cacheand~/.cache/yarn— Yarn 2+ hard-link cache. -
~/.pnpm-store— pnpm's content-addressable store. -
~/.cache/node/ the node-gyp cache.
npm cache clean --force
yarn cache clean
pnpm store prune
brew cleanup also handles some old node-install cruft, but the npm/Yarn/pnpm moves above are the real ones.
Why it comes back (and how to stop it)
Because every fresh npm install rebuilds the folder. Tricks for keeping disk usage sane:
-
Aggressive
.gitignorediscipline — ifnode_modulesgets committed, every clone on this machine rips a fresh copy. - pnpm instead of npm — pnpm dedupes via a global store + hard links, so 30 projects create disk weight for one, not 30.
- Archive, don't hoard — old projects you're not touching belong in git, not wearing 2 GB of installed packages.
See it, then clean what matters
A disk analyzer makes this trivial — sort by size, and the dead Node projects jump to the top in seconds. The point isn't to delete by magic; it's to see the map and decide.
DiskInsight scans your disk, ranks every folder by byte cost (including hidden node_modules, DerivedData, sim caches), and stages anything you choose to the Trash — no irreversible deletion, no rm -rf of the wrong tree. 100% local, no telemetry.
The 30-second fix
- Run the
find+du -shone-liner to see the damage. -
npm cache clean --force(safe, immediate win). -
rm -rfthenode_modulesof archived projects only. - If you host old repos, delete their local clones — git keeps your reality.
Node disk bloat is the most fixable developer disk problem on macOS, bar none.
DiskInsight is a local macOS disk analyzer with nine visualizers; scanning is free forever, and a one-time Pro unlock adds duplicate cleanup, a dev cache cleaner (Xcode, npm, CocoaPods, Gradle) and a full app uninstaller. Nothing is uploaded, no account, no telemetry.
More: https://diskinsight.com
Top comments (0)