How to find and delete every node_modules folder on Mac
Every JavaScript project carries its own node_modules, often 300 MB to 2 GB. Across years of side projects, tutorials and cloned repos, that adds up to tens of gigabytes you'll never use again. The good news: node_modules is always safe to delete. Your lockfile lets you restore it exactly with one command.
1. Find them all, with sizes
find ~/Developer -name node_modules -type d -prune -exec du -sh {} + | sort -h
Replace ~/Developer with wherever you keep your code (for example ~/Projects or ~/code). -prune stops find from descending into nested node_modules, which keeps it fast and avoids counting the same files twice.
To see which projects you haven't touched in a while, list them by last modified date:
find ~/Developer -name node_modules -type d -prune -exec stat -f "%Sm %N" -t "%Y-%m-%d" {} + | sort
2. Delete one project's node_modules
rm -rf ~/Developer/old-project/node_modules
To get it back, run your package manager's install in that project. It reinstalls exactly the versions in your lockfile:
| Lockfile | Restore with |
|---|---|
package-lock.json | npm ci |
pnpm-lock.yaml | pnpm install --frozen-lockfile |
yarn.lock | yarn install --frozen-lockfile (Yarn 1) or yarn install --immutable (Yarn 2+) |
bun.lock | bun install --frozen-lockfile |
Deleting with rm -rf bypasses the Trash. Don't delete node_modules in a project that's running (a dev server or watcher will crash), and check that the project has a lockfile, or a reinstall may pick up newer versions.
3. Don't forget the package manager caches
Deleting node_modules doesn't touch the global download caches, which are often several gigabytes on their own:
| Tool | Cache location on macOS | Clean command |
|---|---|---|
| npm | ~/.npm/_cacache | npm cache clean --force |
| pnpm | ~/Library/pnpm/store | pnpm store prune |
| Yarn 1 | ~/Library/Caches/Yarn | yarn cache clean |
| Yarn 2+ | ~/.yarn/berry/cache | yarn cache clean --all |
| Bun | ~/.bun/install/cache | bun pm cache rm |
pnpm store prune is the gentlest: it removes only packages no project on your Mac references any more. The others empty the cache, so the next install downloads everything again.
Other project folders worth checking
- Rust:
target/in each Cargo project, often several GB;cargo cleanremoves it. - Python: virtual environments (
.venv,venv); recreate them fromrequirements.txtoruv sync. - Swift packages:
.build/in SwiftPM projects, rebuilt byswift build.
Rather see it all at once? Storage Cleaner finds every node_modules folder, Rust target, Python venv and SwiftPM build across your projects, in one scan, shows when you last used each item and whether it's safe to remove, and cleans to the Trash with Undo. Free to scan; one payment of $2.99 to clean, yours for life.
Download freeFrequently asked
Is it safe to delete node_modules? Yes. It contains only downloaded packages. As long as the project has a lockfile, one install command restores exactly the same versions.
Why is node_modules so big? Each project keeps its own copy of every dependency and their dependencies, including native binaries for build tools. npm and Yarn 1 don't share files between projects; pnpm does, via its store, which is why it uses much less space.
Should I add node_modules to Time Machine exclusions? Yes, it's a good idea: it's large, changes constantly and can always be reinstalled. Add your projects' node_modules folders in System Settings › General › Time Machine › Options.