Find and delete old node_modules folders on a Mac
Every JavaScript project keeps its own node_modules folder, and old projects keep theirs forever. To find them, run find on your code folder with -prune, or use npx npkill to browse them by size. Deleting node_modules is safe when the project has a lockfile, because npm install or npm ci brings it back.
Why it gets big
A node_modules folder holds every package a project depends on, plus the packages those depend on. npm and Yarn give each project its own full copy. A small app can need hundreds of megabytes, and a framework project with a test runner and a bundler often needs more.
Nothing ever removes these folders for you. Finish a project, clone a repo to try something, or follow a tutorial, and its node_modules stays on disk until you delete it. Monorepos make it worse, because each package inside packages/ or apps/ can have a node_modules of its own.
The folder is not your work. Your code lives next to it, and the lockfile (package-lock.json, pnpm-lock.yaml, yarn.lock or bun.lock) records the exact versions, so the folder can be rebuilt at any time.
How to clear it by hand
1. List them with their sizes
Go to the folder where you keep code and list every node_modules, biggest last:
cd ~/Code
find . -name node_modules -type d -prune -exec du -sh {} + | sort -h-prune stops find from walking into each node_modules, so you get one line per project instead of thousands of nested folders, and it runs much faster. This command only lists. It deletes nothing.
Search your code folder, not your whole home folder. Apps and editor extensions in ~/Library and ~/.vscode ship their own node_modules, and those are part of the app.
2. Check the project is really old
Before deleting, see when you last committed to it:
git -C ~/Code/old-project log -1 --format=%crAlso check that a lockfile sits next to the folder. Without one, a reinstall may pick newer versions.
3. Delete the ones you do not need
rm -rf ~/Code/old-project/node_modulesDelete them one path at a time from your list. rm -rf skips the Trash, so check each path before you press Return. If you prefer the Trash, drag the folder there in Finder instead.
When you come back to the project, restore the dependencies from the lockfile:
npm ci # or: pnpm install, yarn install, bun installOr use npkill
npx npkill scans from the current folder and shows every node_modules with its size. Move with the arrow keys and press Space to delete the selected one. It deletes straight away, without the Trash, so read the path first.
The shared caches
- npm cache in
~/.npm: downloaded package tarballs. Safe to clear; npm downloads again on the next install. - pnpm store: one shared copy of each package, linked into projects.
pnpm store pruneremoves packages no project uses. - Yarn and Bun caches in
~/Library/Caches/Yarnand~/.bun/install/cache: also safe, re-downloaded on demand.
How Berth handles it
Berth finds your git repositories in the usual code folders (~/Code, ~/Projects, ~/Developer, ~/playground and others) and in project folders directly in your home folder. For each one it reads the node_modules at the top, plus the ones inside packages/, apps/, services/ and libs/ in a monorepo.
- Yours, kept: your source code. Counted as part of the project, never selected.
- Rebuilds itself: node_modules, shown as its own part of each project’s size.
- Re-downloadable: the npm, Yarn, pnpm and Bun caches, and the pnpm store.
Berth only suggests removing node_modules from “sleeping” projects: no commits in 90 days or more, and a lockfile present, so the same versions come back. It estimates about a minute per project to reinstall. Active projects keep their dependencies so your work stays fast.
What it does not cover: node_modules in folders that are not git repositories, and very large code folders, where Berth reads up to 60 of your biggest repositories. With Berth Pro you can also archive a whole sleeping project to an external drive.
Whatever Berth finds goes into a review tray first. A dry run shows exactly what would move, pre-flight checks make sure the owning app is closed, and everything goes to the macOS Trash, so Undo puts it back. Berth is free to scan and explore; one-click cleanup is part of Berth Pro, $14.99 one-time for 2 Macs. There is no account and nothing is uploaded.
Questions
Is it safe to delete node_modules?
Yes, as long as the project has a lockfile. node_modules only holds downloaded packages. Running npm ci or npm install in the project puts them back.
How do I find all node_modules folders on my Mac?
In Terminal, go to your code folder and run find . -name node_modules -type d -prune -exec du -sh {} + to list each one with its size. Or run npx npkill.
Why is node_modules so big?
It holds every dependency and every dependency of those, and npm gives each project its own copy. Build tools, test runners and native binaries add up quickly.
Does deleting node_modules break my project?
Not permanently. The project will not run until you reinstall, which takes about a minute for most projects and needs an internet connection unless the packages are cached.
Should I also clear the npm cache?
Only if you need the space. The cache makes reinstalls faster. Clearing it is safe, and npm downloads packages again when needed.
