guide
How to delete node_modules on a Mac (all of them, safely)
Quick answer
Yes, it's safe. node_modules is rebuilt from package.json and your lockfile. For one project: rm -rf node_modules in its folder. For every project under a folder, list them first, then delete:
find ~/Projects -name node_modules -type d -prune
find ~/Projects -name node_modules -type d -prune -exec rm -rf {} +Bring one back with npm ci, pnpm install, yarn install or bun install, whichever lockfile the project has.Why node_modules gets so big
Every JavaScript project keeps its own copy of every dependency, and frameworks like React Native, Next.js and Expo pull in thousands of packages. On the Mac Sweep was built on, 86 node_modules folders took 76.8 GB; the biggest single one, a React Native app, was 7.4 GB. Most belonged to projects nobody had opened in months.
Delete one project's node_modules
cd ~/Projects/old-app
rm -rf node_modules
Or drag the folder to the Trash in Finder. The Trash doesn't free any space until you empty it.
Delete every node_modules under a folder
- List them first.
-prunestopsfindfrom going inside each match, which is what makes it fast and avoids listing the nestednode_modulesinside packages:find ~/Projects -name node_modules -type d -prune - Check the list. Leave out projects you're working on today: deleting their
node_moduleswhile a dev server or Metro is running breaks it until you reinstall. - Delete.
This deletes right away, without the Trash.find ~/Projects -name node_modules -type d -prune -exec rm -rf {} +
Get it back
| Lockfile in the project | Command |
|---|---|
package-lock.json | npm ci (exact versions from the lockfile) |
pnpm-lock.yaml | pnpm install |
yarn.lock | yarn install |
bun.lock or bun.lockb | bun install |
| none | npm install |
In a monorepo (a pnpm-workspace.yaml, or workspaces in the root package.json), run the install once in the root, not in each package.
Before you delete
- Very old projects may not install again exactly. Packages get unpublished and old versions stop building with a newer Node. If a project matters, try reinstalling it before you need it.
- Reinstalling takes time and network. A big React Native project is a few minutes.
- pnpm frees less than it looks. pnpm hard-links
node_modulesinto a shared store, so most of the space only comes back afterpnpm store prune. - Don't run the delete over your whole home folder. Point it at the folder your projects live in.
Questions
Is it safe to delete node_modules?
Yes. It only contains packages downloaded from the registry, and npm, pnpm, yarn or bun reinstall them from package.json and the lockfile. Your own code is never in node_modules.
Will I lose anything?
Only if someone edited files inside node_modules by hand (a patch without patch-package). Otherwise no: the next install brings back exactly what the lockfile says.
Should I use npm ci or npm install afterwards?
npm ci when there is a package-lock.json: it installs exactly the locked versions and is meant for a clean node_modules. npm install may update the lockfile.
Why does find list fewer folders with -prune?
Without -prune, find also walks inside every node_modules and lists the nested node_modules of each package, thousands of them. With -prune you get one line per project.
Does deleting node_modules break git?
No. node_modules is normally in .gitignore, so git doesn't see it either way.