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

  1. List them first. -prune stops find from going inside each match, which is what makes it fast and avoids listing the nested node_modules inside packages:
    find ~/Projects -name node_modules -type d -prune
  2. Check the list. Leave out projects you're working on today: deleting their node_modules while a dev server or Metro is running breaks it until you reinstall.
  3. Delete.
    find ~/Projects -name node_modules -type d -prune -exec rm -rf {} +
    This deletes right away, without the Trash.

Get it back

Lockfile in the projectCommand
package-lock.jsonnpm ci (exact versions from the lockfile)
pnpm-lock.yamlpnpm install
yarn.lockyarn install
bun.lock or bun.lockbbun install
nonenpm 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_modules into a shared store, so most of the space only comes back after pnpm 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.