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.
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
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 -20For 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
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 ~/.npmnpm 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
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 pathpnpm 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
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 dirbun 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
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 -gLeave 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
JavaScript leftovers are the easiest kind to clear, because nearly all of them answer yes to the second question:
- Is its cause still alive? The project you’re on, a running dev server, your global tools: leave them.
- 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.