Finding Every node_modules Folder on Your Mac, and the Caches Behind Them

The heaviest part of an old JavaScript project is rarely your code. It’s a download you already did, sometimes twice.

A single old project split into its parts: source code a few megabytes, node_modules several hundred megabytes, and a build folder
The heavy part of an old project is rarely the work itself. Example data, not a real Mac.

You open a project you haven’t touched since spring, run du out of curiosity, and the number is absurd. Your code is a few megabytes. The folder is nine hundred. Multiply that by every tutorial, prototype and client repo on your Mac and you’ve found where a good chunk of your disk went.

None of this is a bug. Every JavaScript project downloads its dependencies into its own node_modules, and every package manager keeps a second copy in a global cache so the next install is faster. Both are effects of a cause: a project you were working on. When the project ends, the effects stay.

The good news is that this is the friendliest kind of leftover. Almost all of it comes back with one command. The work is knowing which copies are dead, and which tool owns each cache.

1. node_modules in projects you closed

Difficulty1 of 5

A Terminal window listing node_modules folders across a Projects folder, sorted by size, the largest over a gigabyte
Every node_modules folder under one folder, biggest last. Example output.

Start by finding them. Point this at wherever you keep your projects. It only reads, and -prune stops it from descending into nested node_modules:

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

For a project you’ve finished or paused, the folder is safe to delete as long as its package.json and lockfile are there. When you come back, one install rebuilds it exactly, from the lockfile:

npm ci

(pnpm install, yarn install and bun install do the same for their projects.) Move the folders to the Trash rather than deleting them in place, so a wrong pick costs you a click.

A dependency folder nobody runs is just a download you already did.

2. The npm cache

Difficulty2 of 5

The ~/.npm folder with its _cacache directory growing over months as new package versions are installed
npm’s cache only ever grows. Example data, not a real Mac.

npm keeps a content-addressed cache in ~/.npm/_cacache. Its documentation is refreshingly direct about two things: npm “will not remove data by itself”, so the cache grows with every package version you install, and the cache is self-healing, so you never need to clear it to fix a problem. The only good reason to clear it is disk space.

See how big it is:

du -sh ~/.npm

npm cache verify garbage-collects data npm no longer needs and checks the rest, which is a gentle first step. If you want it all gone, npm cache clean --force empties it; the --force is npm making sure you mean it. Nothing breaks: npm downloads packages again the next time a project asks.

A cache that never shrinks on its own needs you to do it, once in a while.

3. The pnpm store

Difficulty3 of 5

Diagram of a shared pnpm store linked into three projects; one package version is no longer linked by any project and is marked unreferenced
pnpm keeps one copy and links it into every project. Illustration.

pnpm works differently, and it changes what “delete node_modules” buys you. It keeps one copy of each package version in a shared store and links it into each project. That’s why pnpm projects take less space, and also why deleting one project’s node_modules can free much less than du suggested: the files are still in the store.

Find the store, then see its size:

pnpm store path

pnpm store prune removes packages that no project on your Mac references anymore. pnpm’s own docs call it harmless, with one honest caveat: if you later switch to an old branch that needs a removed version, pnpm downloads it again. Run it after a round of deleting old projects, not every day.

With pnpm, delete the projects first, then prune the store.

4. Yarn and Bun caches

Difficulty3 of 5

Three cache folders side by side, npm, Yarn and Bun, each with the command that shows its location
Each package manager keeps its own cache, in its own place. Example data, not a real Mac.

If you’ve used more than one package manager, you have more than one cache. Each tool knows where its own is; ask it rather than guessing a path. These only print the location:

yarn cache dir
bun pm cache

(yarn cache dir is Yarn 1. In Yarn 2 and later, yarn config get cacheFolder shows the global cache.) To clear them, use the tools themselves: yarn cache clean in Yarn 1, yarn cache clean --all in later versions, and bun pm cache rm for Bun. Like npm’s cache, all of these come back as you install.

Every package manager can empty its own cache. Let it.

5. The node_modules that are still alive

Difficulty4 of 5

A list of node_modules folders where the current project, a running dev server and the global npm folder are marked alive, and two old tutorial projects are marked safe to remove
Big and boring-looking, but in use. Example data, not a real Mac.

Not every node_modules is a leftover. The one in the project you’re building today is alive. So is one under a dev server running in a terminal tab you forgot about: delete it mid-run and you’ll spend ten minutes on a very confusing error.

The one to be careful with is the global folder, where npm install -g puts command-line tools. It’s also called node_modules, it shows up in searches, and deleting it removes tools you use every day. See where it is:

npm root -g

Leave it out of any cleanup. If a global tool is no longer needed, uninstall it by name with npm uninstall -g.

A node_modules folder with a running process or a global tool inside is not a leftover.

Conclusions

Decision diagram with two questions in order: is its cause still alive? If yes, leave it. If no, does it come back if removed? If yes, let it go; if no, look twice before you clear
The same two questions, applied to JavaScript.

JavaScript leftovers are the easiest kind to clear, because nearly all of them answer yes to the second question:

  1. Is its cause still alive? The project you’re on, a running dev server, your global tools: leave them.
  2. If I remove it, does it come back? node_modules: yes, from the lockfile. npm, pnpm, Yarn and Bun caches: yes, as you install.

If the project is over, its node_modules can go. The lockfile remembers everything.

If you’d like every project on your Mac listed with its node_modules, build folders and how long it’s been idle, Haikos Clean does that scan for you, leaves projects that are open in an editor alone, and moves only what you pick to the Trash. For Xcode’s side of the same story, read What Xcode leaves on your Mac.

Questions people ask

Is it safe to delete node_modules?

Yes, for any project with a package.json and a lockfile. node_modules only holds downloaded packages, and npm install, pnpm install, yarn or bun install recreates it from the lockfile. Delete it for projects you have finished or paused. Avoid deleting it while a dev server or build is running in that project, and check that the lockfile is committed first.

Does npm cache clean free up space?

It can, because npm never removes cached data by itself and the cache in ~/.npm grows with every new package version you install. npm calls the cache self-healing, so clearing it is only worth doing to reclaim disk space, which is why npm cache clean requires --force. Your projects keep working; npm downloads packages again as needed.

Why did deleting a pnpm project’s node_modules free so little space?

pnpm keeps one copy of each package version in a shared store and links it into every project that uses it. Deleting one project’s node_modules removes the links, but the files stay in the store while any project, or nothing at all, still references them. Run pnpm store prune afterwards to remove packages that no project uses anymore.

Sources

Paths and commands were checked against these pages on the date above.

Mac storage

How to Check What’s Using Storage on a Mac

Where to check storage on a Mac in every recent macOS version, what the categories mean, and how to measure the large folders Storage does not name.

5 min read

All guides