How do I find and delete all node_modules folders on a Mac?

A node_modules folder holds a project's installed dependencies and can be rebuilt from package.json and the lockfile, so the ones in old projects are usually safe to delete.

Shiva ChandraMaker of Clean House · · 4 min read
Short answer

To find node_modules on a Mac, list them first with find in your projects folder and -prune, check sizes with du, then delete the ones in finished projects. Each rebuilds with npm install, yarn or pnpm install. Then clear package manager caches separately.

Why does node_modules use so much space?

Every JavaScript project installs its own copy of its dependencies, and each dependency brings its own. A single project's node_modules is often hundreds of megabytes, and a developer with dozens of old projects can have tens of gigabytes spread across a disk. Because the folder is generated from package.json and the lockfile, it is among the safest large things to remove from a project you are not working on.

How do I list every node_modules folder first?

List before you delete. This command searches a folder you name, prints each node_modules it finds, and does not descend into them (that is what -prune does, and it also keeps nested copies from being listed over and over):

find ~/Projects -name node_modules -type d -prune -print

Change ~/Projects to where you keep code. To see sizes, largest last:

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

Read the list. Each line is a project you either still use or don't. Delete one by hand if you want to be careful:

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

How do I delete all of them at once?

Only after you have read the list. The same find with an action added:

find ~/Projects -name node_modules -type d -prune -exec rm -rf {} +
This permanently deletes every node_modules folder under that path, skipping the Trash, and cannot be undone. Never point it at your home folder or at /, where it would hit folders inside apps and tools. Make sure no dev server, build or editor task is running in those projects.

Exclude a project you are using by running it per folder, or just delete the folders for finished projects with the one-by-one command above. To get a project back: npm ci (installs exactly what the lockfile says), yarn install or pnpm install. You need a network connection unless the package manager's cache already has everything.

A few cases to check before deleting: a package you patched by editing files inside node_modules, a project whose lockfile is missing, and work you must do offline. Detail on the folder is at node_modules folders.

How do I clear the npm, Yarn and pnpm caches?

These are separate from node_modules. They hold downloaded package files so reinstalls are fast. Each can be deleted and is refilled on demand.

ToolFind the folderClear it
npmnpm config get cache (default ~/.npm)npm cache verify, or npm cache clean --force
Yarn (classic)yarn cache diryarn cache clean
pnpmpnpm store pathpnpm store prune

npm's documentation says the cache is self-healing, that npm cache clean requires --force, and that you normally only need it to reclaim disk space. npm cache verify checks the cache and removes unneeded data, so try it first. pnpm store prune removes packages that no project uses anymore; pnpm's docs call it harmless but suggest not running it too often, because switching branches may need packages again. Details: npm cache, Yarn cache and pnpm store.

npm cache clean --force and yarn cache clean delete the cache outright. The next install downloads packages again. Nothing in your projects changes.

If you use pnpm, note that its node_modules folders are built from links into the shared store. Deleting them frees less than du reports, and the store is where the real files live. Newer Yarn versions (Berry) may keep a cache inside each project instead, so check yarn cache dir before assuming a location.

How do I stop node_modules from filling the disk again?

  • Delete node_modules when you finish with a project, and keep the lockfile in git.
  • Prefer pnpm for many projects, since it stores each package version once.
  • Run the size listing above every few months.

How does Clean House handle node_modules?

Clean House recognizes node_modules folders as developer data and lists them with their size, the project they belong to and when that project was last opened. The "Why?" card says what happens if one goes: it comes back with an install. Each gets a call: Safe to clear, Look first or Keep. It avoids a project you are using right now. Cleaning moves items to the Trash after a re-check, and Recently Cleared can put them back, so you don't need to write a find command or worry about the wrong path.

The same approach applies to Xcode, Gradle and Docker data.

Questions

Is it safe to delete node_modules?

Yes, for projects you can reinstall. Run npm install, yarn or pnpm install afterward and the folder comes back from package.json and the lockfile. Keep it if you are offline or the project has patched or hand-edited packages.

How do I find all node_modules folders on my Mac?

Run find from your projects folder with -name node_modules -type d -prune, which lists each one without looking inside. Add du -sh to see sizes. Do not run it from your whole home folder.

What does npm cache clean --force do?

It deletes npm's download cache, normally in ~/.npm/_cacache. Packages are fetched again on the next install. npm's docs say this is usually unnecessary because the cache is self-healing.

Does deleting node_modules free as much space as du says with pnpm?

Often less. pnpm links files from a shared store instead of copying them, so a project's node_modules can look bigger than the space it uniquely uses.

Why are there node_modules folders inside other folders?

Some apps, tools and old projects ship or install their own. Only delete ones inside projects you own. Leave those inside .app bundles or system tools alone.

Sources

  1. npm Docs: npm cache
  2. pnpm Docs: pnpm store