Developer guide

How to clean node_modules and npm, pnpm, Yarn and Bun caches on a Mac

Clear npm, pnpm, Yarn and Bun caches and delete old node_modules folders on a Mac without breaking projects.

4 safe1 careful1 never

updated read 4 minfor npm 11, pnpm, Yarn 1 and Yarn Berry, Bun on macOS

The short answer

  1. 01

    npm cache is ~/.npm. Clear it with npm cache clean --force. npm says the only reason is reclaiming disk space.

  2. 02

    pnpm store: pnpm store path shows it (default ~/Library/pnpm/store). pnpm store prune removes unreferenced packages.

  3. 03

    Yarn 1: yarn cache dir, then yarn cache clean. Yarn 2 and later use ~/.yarn/berry/cache. Bun: bun pm cache rm.

  4. 04

    node_modules is safe to delete in any project you can reinstall. Keep package.json and the lockfile, then run npm ci or pnpm install.

  5. 05

    Do not delete the whole pnpm store by hand. Projects hard-link into it, so every project downloads again.

Package manager cachesIllustration. Nothing here runs a command.

npm

Cache location~/.npmData lives in _cacache inside it.

See path or sizenpm config get cachePrints the cache root. Removes nothing.
Cleannpm cache clean --forceRemoves: Cached downloads. The next install fetches them again. npm requires --force.

Preview only. Nothing runs.

pnpm

Cache location~/Library/pnpm/storeDefault on macOS when PNPM_HOME and XDG_DATA_HOME are not set.

See path or sizepnpm store pathPrints the active store. Removes nothing.
Cleanpnpm store pruneRemoves: Only packages no project references. Projects keep working. Do not delete the whole store by hand: projects hard-link into it.

Preview only. Nothing runs.

Yarn

Cache location~/Library/Caches/YarnYarn 1. Yarn 2 and later use ~/.yarn/berry/cache by default.

See path or sizeyarn cache dirYarn 1: prints the cache folder. Removes nothing.
Cleanyarn cache cleanRemoves: Cached packages (add a package name to clear one). On Yarn 2 and later, --mirror removes the global ones and --all removes both.

Preview only. Nothing runs.

Bun

Cache location~/.bun/install/cacheUnless BUN_INSTALL_CACHE_DIR is set.

See path or sizebun pm cachePrints the cache path. Removes nothing.
Cleanbun pm cache rmRemoves: The Bun global cache. Packages download again when needed.

Preview only. Nothing runs.

node_modules finder

Five example projects on an example Mac. Old ones are highlighted.

  • ~/code/shop-web412 MBidle 14 months
  • ~/code/blog288 MBidle 9 months
  • ~/code/api-gateway531 MBtouched 3 weeks ago
  • ~/code/landing196 MBtouched 5 days ago
  • ~/code/old-experiment640 MBidle 26 months

3 projects, 1.34 GB of node_modules you could reinstall from a lockfile.

List every node_modules with its sizefind ~ -name node_modules -type d -prune -exec du -sh {} +-prune stops it descending into nested ones. Removes nothing. Or run npx npkill. Keep package.json and the lockfile, then reinstall with npm ci or pnpm install. Leave node_modules that belong to installed apps.
How npm, pnpm, Yarn and Bun caches differ, and how to spot old node_modules folders, on an example Mac.

Package caches and node_modules are different

To clear the npm cache on a Mac you run one command, but disk space comes from two places.

  • Package cache: a shared download store per tool.
  • node_modules: a full copy of dependencies inside each project.

Both can be recreated, so cleaning is low risk. For the rest of your Mac, see the guide to freeing disk space.

Safe to delete?

ItemVerdictWhat you lose
npm cache (~/.npm/_cacache)SafeCached downloads. Next install fetches again.
Yarn and Bun cachesSafeSame: packages download again.
pnpm store, via pnpm store pruneSafeOnly packages no project references.
node_modules of an old projectSafeInstalled dependencies. Reinstall from the lockfile.
Whole pnpm store deleted by handCarefulEvery project re-downloads on its next install. Files already linked keep working.
node_modules inside an installed appNeverThe app can stop working. npkill warns about this too.

npm

npm config get cache

Prints the cache root, normally ~/.npm. Removes nothing.

npm cache verify

Garbage-collects unneeded entries and checks integrity. Safe and offline.

npm cache clean --force

Empties the cache. npm requires --force. npm never prunes the cache itself, so it only grows.

pnpm

pnpm store path

Prints the active store. On macOS the default is ~/Library/pnpm/store when PNPM_HOME and XDG_DATA_HOME are not set.

pnpm store prune

Removes packages not used by any project on the system. pnpm says it has no side effects on projects; run it occasionally, not often.

pnpm hard-links packages from the store into each project, so deleting the whole store folder makes every project download again. Prefer prune.

Yarn

yarn cache dir

Yarn 1: prints the cache folder, usually ~/Library/Caches/Yarn.

yarn cache clean

Yarn 1: clears the cache. Add a package name to clear just that one.

Yarn 2 and later (Berry) default to a global cache at ~/.yarn/berry/cache. There, yarn cache clean removes local archives, --mirror the global ones, and --all both.

Bun

bun pm cache

Prints the cache path, by default ~/.bun/install/cache.

bun pm cache rm

Clears the Bun cache.

Find and delete node_modules

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

Lists every node_modules folder under your home with its size. -prune skips nested ones. Removes nothing. It can be slow on a large home folder.

npx npkill

An interactive tool that lists node_modules folders with sizes and deletes the ones you pick. Use -d to set the start folder.

rm -rf path/to/project/node_modules

Deletes one project's dependencies permanently. Reinstall with npm ci or pnpm install.

Some installed apps ship their own node_modules and need them to run. Only delete folders inside your own project directories. Check that you have a lockfile first, because without it a reinstall may pick different versions.

Good candidates are projects you have not opened in months. Old build output such as .next or dist in those projects can usually go too, but only if git-ignored and rebuilt by a command you know.

Skip the guesswork on stale projects

Hoardless cleans the npm cache (~/.npm/_cacache, Auto tier, npm cache clean --force), Yarn (~/Library/Caches/Yarn, Auto, yarn cache clean) and the pnpm store (Auto, pnpm store prune). Without the tool it moves npm and Yarn caches to the Trash, and the pnpm store at Confirm tier. Its Unused view lists node_modules in git repos idle for 90 days, where the folder is git-ignored, untouched for 90 days and at least 20 MB, then moves it to the Trash after you approve. It does not detect Yarn Berry or Bun caches.

See Hoardless

Questions

Is it safe to delete node_modules?

Yes, for a project you can reinstall. Keep package.json and the lockfile and run npm ci or pnpm install later. Leave node_modules folders that belong to installed apps.

Is it safe to run npm cache clean --force?

Yes. npm's documentation says the cache is self-healing and clearing it is only needed to reclaim disk space. Packages download again when needed.

Where is the npm cache on a Mac?

At ~/.npm, with package data in ~/.npm/_cacache. npm config get cache prints the configured location.

What does npm cache verify do?

It garbage-collects unneeded data and verifies the index and cached data, without wiping the cache.

How do I find all node_modules folders?

Run find ~ -name node_modules -type d -prune -exec du -sh {} + or use npx npkill, which lists them with sizes.

How do I clear the pnpm store?

Run pnpm store prune to remove packages no project uses. pnpm store path shows where the store is.

Where is the Yarn cache on a Mac?

For Yarn 1, run yarn cache dir. It is usually ~/Library/Caches/Yarn. Yarn 2 and later use ~/.yarn/berry/cache by default.

How do I clear the Bun cache?

Run bun pm cache rm. The cache is at ~/.bun/install/cache unless BUN_INSTALL_CACHE_DIR is set.