How to

How to find and delete node_modules (and clear npm, pnpm, Yarn and Bun caches) on a Mac

Where node_modules and package manager caches live, which ones are safe to delete, and the commands that free space without breaking the projects you still use.

Free to scan. Seven days of cleaning free, no card. macOS 26 or later.

Two different piles of JavaScript dependencies fill developer Macs. One is node_modules inside each project. The other is the shared cache each package manager keeps in your home folder. Both can be recreated. Treat them separately.

Project folders: node_modules

node_modules is the installed copy of whatever package.json and the lockfile describe. Deleting it loses none of your source. The next install puts it back.

Before you delete:

  • Stop any dev server or build running in that project.
  • Check that a lockfile is present (package-lock.json, pnpm-lock.yaml, yarn.lock or bun.lock). Without one, a reinstall may resolve newer versions than you had.
  • Only delete folders inside your project directories. Some apps ship their own node_modules and need them to run.

List sizes under a project's root (slow on a whole home folder):

find ~/code -name node_modules -type d -prune -exec du -sh {} +

Delete one you no longer need:

rm -rf ~/code/old-project/node_modules

Reinstall later from the lockfile:

npm ci

or pnpm install --frozen-lockfile

or yarn install --frozen-lockfile (Yarn 1), yarn install --immutable (Yarn 2 and later)

or bun install --frozen-lockfile

pnpm is a special case

pnpm hard-links packages from a shared store into each project's node_modules. du overstates what deleting one node_modules will free, because the bytes still live in the store. After removing stale projects, run:

pnpm store path

pnpm store prune

pnpm store prune removes packages no project references any more. That is usually where the space actually comes back.

Shared caches

Package managers keep download caches so the next install does not hit the network for everything. They grow until you prune them. Clearing them is safe; the cost is a slower next install.

ToolTypical location on macOSClear
npm~/.npm/_cacachenpm cache clean --force
pnpmthe store pnpm store path reportspnpm store prune
Yarn (classic)~/Library/Caches/Yarnyarn cache clean
Yarn Berry~/.yarn/berry/cacheyarn cache clean --all, run inside a Yarn 2+ project
Bun~/.bun/install/cachebun pm cache rm

The global yarn is often Yarn 1, which only clears the classic cache. Yarn 2 and later run per project, so clear the Berry cache from inside a project that uses it.

npm's docs say the cache is self-healing and that clearing it is mainly for reclaiming disk. Start with npm cache verify if you only want garbage collection; use npm cache clean --force when you want it empty.

Check sizes first:

du -sh ~/.npm/_cacache ~/.npm/_npx "$(pnpm store path)" ~/.bun/install/cache ~/Library/Caches/Yarn ~/.yarn/berry/cache 2>/dev/null

npx keeps packages you have run with npx under ~/.npm/_npx. That folder is separate from _cacache and can be large on its own.

Doing it with DevSweeper

Two views cover this:

  • Project folders: add the directories you keep projects in. DevSweeper lists node_modules next to package.json (and the matching folders for Python, CocoaPods, Rust, Gradle, Flutter and the common JS frameworks), marks stale projects by your staleness window, and names the install command that brings each one back, picked from the project's lockfile.
  • Caches, Docker and Android: measures npm, pnpm, Yarn, Bun and the other package caches, counts only the space removal would free (blocks shared with APFS clones are left out), and clears each with the tool's own command, or moves it to the Trash where no command reaches it, as with the Yarn Berry cache.

Free to scan; seven days of cleaning free, no card. For agent worktrees that each grew their own node_modules, see how to clean up agent worktrees.

More of what DevSweeper finds