How to find and delete old node_modules folders and package caches on a Mac
Every JavaScript project carries its own node_modules, often 200 MB to 1 GB, and package managers keep a second copy of everything in their caches. Projects you finished months ago still hold all of it.
The short answer: node_modules folders and package caches are safe to delete: npm install (or pnpm, yarn, bun) brings back exactly what the lockfile lists. Use each package manager's own clean command for its cache.
How do I find all node_modules folders?
Point this at the folder where your code lives. It lists every node_modules with its size, biggest last:
find ~/Projects -type d -name node_modules -prune -exec du -sh {} + 2>/dev/null | sort -h
-prune keeps it from counting the node_modules inside other node_modules. Move the ones of old projects to the Trash in Finder, or delete them with rm -rf (no undo).
Is it safe to delete node_modules?
Yes. It holds downloaded packages, and your package.json and lockfile say which ones. Running npm install in the project restores them. Two exceptions: packages you edited by hand inside node_modules (use patch-package or your package manager's patch command instead) and working offline.
Which package caches can I clear, and how?
- npm (
~/.npm/_cacache):npm cache clean --force - pnpm (
~/Library/pnpm/store):pnpm store pruneremoves packages no project uses - Yarn (
~/Library/Caches/Yarn,~/.yarn/berry/cache):yarn cache clean - Bun (
~/.bun/install/cache):bun pm cache rm - Homebrew (
~/Library/Caches/Homebrew):brew cleanup - pip (
~/Library/Caches/pip):pip cache purge; uv (~/.cache/uv):uv cache clean - Go:
go clean -cache -modcache - CocoaPods:
pod cache clean --all; Gradle (~/.gradle/caches), Maven (~/.m2/repository) and Cargo (~/.cargo/registry) can go to the Trash and download again - Playwright browsers (
~/Library/Caches/ms-playwright): reinstall withnpx playwright install
The next install of each project downloads what it needs again, so the first one after clearing is slower.
What about build folders?
Build output rebuilds itself too: .next, .turbo, dist and build in JavaScript projects, target in Rust and Maven, .build in Swift packages, Pods in CocoaPods projects (pod install restores them).
What should I keep?
- Python virtual environments (
.venv): a rebuilt one may differ, since packages installed by hand and versions that aren't pinned don't come back the same. - The Node.js version you use in nvm or fnm. Older versions in
~/.nvm/versions/nodethat no project needs can go. - Projects with uncommitted changes: before removing anything bigger than dependencies, check
git status.