Blog · Developers

How do I delete node_modules on a Mac?

Find them with `find ~/Projects -name node_modules -type d -prune`, check their sizes, and delete the ones belonging to projects you are not working on. Nothing is lost that `npm install` will not put back — but the project will not run until you do.

Every JavaScript project carries its own node_modules, and a few hundred megabytes each adds up fast. A folder of old experiments, tutorials and abandoned prototypes can easily hold tens of gigabytes of dependencies for code nobody will run again.

Is it safe to delete node_modules?

Yes. node_modules is installed, not written: everything in it came from the registry and is described by your package.json and lockfile. Deleting it loses nothing that cannot be fetched again.

What it costs is time and a working project. Until you reinstall, the project will not build or run, and the reinstall needs a network connection. So the sensible targets are projects you are not working on this week.

Find every node_modules folder, largest last

From Terminal, swapping ~/Projects for wherever you keep code:

find ~/Projects -name node_modules -type d -prune -exec du -sh {} + | sort -h

-prune matters: it stops find descending into a node_modules it has already found, so the packages nested inside it are not listed as dozens of separate folders.

Delete them

One project:

rm -rf ~/Projects/old-prototype/node_modules

Every one under a folder — read the list from the command above first, because this does not ask:

find ~/Projects -name node_modules -type d -prune -exec rm -rf {} +

To get one back, run npm install (or yarn, or pnpm install) in that project.

Two things that catch people out

  • The npm cache is somewhere else. npm keeps a download cache in ~/.npm/_cacache, separate from every project’s node_modules. Deleting project folders does not touch it, and it grows on its own. npm cache clean --force clears it; the next install re-downloads what it needs.
  • With pnpm, deleting node_modules frees less than it looks. pnpm links packages from one shared store rather than copying them into each project, so the space mostly lives in the store. Run pnpm store prune afterwards to release packages no project uses any more.

Letting RefreshYourMac find them

RefreshYourMac lists these as Project Build Folders: node_modules along with the other folders a project rebuilds — CocoaPods’ Pods, Flutter’s .dart_tool, .next, dist, Rust’s target, Swift’s .build and more. Each one is sized separately, and each item’s note says how to restore it — npm install, pod install, flutter pub get.

They are rated Review rather than Safe to Remove, because removing one breaks that project until you reinstall, and the confirmation says so before anything is deleted. The npm, Yarn and pnpm download caches are separate rows, rated Safe to Remove, because they really are just caches.

One limit worth knowing: the app looks for project build folders inside ~/Projects. If your code lives somewhere else, the find command above is the way to cover it.

For everything else a developer’s Mac fills up with, see what fills up a developer’s Mac.