node_modules eating your disk? Find and clear them on Mac
node_modules folders are safe to delete in any project you are not working on right now. They contain only downloaded dependencies, and the install command recreates them. One Terminal command lists every one under your projects folder by size, so you can see which are worth removing.
The package managers also keep download caches outside your projects. Those can be cleared too, with each tool's own command.
Why node_modules takes so much space
With npm and Yarn, each project gets its own complete copy of every dependency, and of everything those dependencies need. A small web app can pull in a very large tree of packages. Ten old projects mean ten separate copies.
The folders are also made of an enormous number of small files, which is why they are slow to copy, slow to back up and slow to index. Nothing in them is yours. The list of what to install is in package.json and the lockfile, and those are what you keep.
List every node_modules folder by size
The command below searches a folder for node_modules directories, measures each one, and sorts the list with the largest last. It only reads. Replace ~/Projects with the folder where you keep your code.
Point it at your code folder, not at your whole home folder. Apps and editors keep node_modules folders of their own inside ~/Library and inside application bundles, and those are part of the app, not leftovers. Deleting them breaks the app.
find ~/Projects -type d -name node_modules -prune -exec du -sh {} + 2>/dev/null | sort -hDecide which ones to delete
Deleting node_modules costs you only the time of the next install, so the question is which projects you will open soon.
- Projects you have not touched in months: delete. This is where most of the space is.
- Cloned repositories you only read or tried once: delete.
- Projects you are working on this week: leave them. You would only reinstall straight away.
- Projects without a lockfile: think first. A fresh install may resolve to newer versions of some packages than the ones you had, which can change behaviour.
- Projects that depend on a private registry or packages that may no longer be published: make sure you can still install before deleting.
Delete them safely
Stop any dev server or build running in the project first. Then, in Finder, open the project folder and move node_modules to the Trash. That is the forgiving way: if the reinstall fails, you can put the folder back before emptying the Trash.
When you return to the project, run the install command for the package manager it uses, shown below for npm. If the project has a package-lock.json, npm ci installs exactly the versions recorded in it.
Other build output in the same projects can go the same way: folders such as .next, dist, build and .turbo are recreated by the next build.
npm installClear the package manager caches
Outside your projects, each package manager keeps a cache of everything it has downloaded, so repeat installs are fast. These caches only grow. The commands below are the documented ways to clear them, and each deletes that tool's download cache only. Your projects keep working, and the next install downloads again.
- npm keeps its cache in ~/.npm. npm asks for --force because it treats the cache as self-healing. To check it without clearing, run npm cache verify.
- Yarn 1 keeps a global cache, and yarn cache clean empties it. On newer Yarn versions, run it inside a project.
- pnpm keeps a shared store. pnpm store prune removes only packages that no project on your Mac references any more.
- Bun keeps a global cache, and bun pm cache rm empties it.
npm cache clean --force
yarn cache clean
pnpm store prune
bun pm cache rmUse less space in the first place
pnpm stores each version of a package once on the disk and links it into every project that uses it, so ten projects with the same dependencies take little more than one. If node_modules is a recurring problem, switching new projects to pnpm is the lasting fix.
A side effect is that sizes look wrong. A tool that adds up file sizes counts a linked package once per project, so a pnpm node_modules folder can appear much larger than the space it really uses.
It also helps to keep code out of synced folders. A node_modules folder inside iCloud Drive or Dropbox means a huge number of small files being uploaded for no benefit.
How Diskzy handles node_modules
Diskzy's Clean up tab finds project build folders, including node_modules, .build, Pods and virtual environments, wherever they are on the disk. A folder is left unticked if its project changed in the last 14 days, so the projects you are working on are not touched unless you choose them.
The npm, Yarn, pnpm and Bun caches are listed and ticked. Everything removed goes to the Trash, so a folder can be put back until you empty it.
Common questions
- Is it safe to delete node_modules?
- Yes, inside your own projects. It holds only downloaded dependencies, and npm install, yarn install or pnpm install recreates it. Do not delete node_modules folders inside apps or inside ~/Library.
- How do I find all node_modules folders on my Mac?
- Run find with -name node_modules and -prune on the folder where you keep your code, and pass each result to du -sh to see its size. The full command is in this guide.
- Will deleting node_modules break my project?
- No. The project will not run until you install again, and then it works as before. With a lockfile, the same versions are installed.
- Is it safe to clear the npm cache?
- Yes. The cache is a local copy of packages npm has already downloaded. npm fetches whatever is missing from the registry on the next install.
- Why is my node_modules folder bigger than my whole project?
- Because it contains every dependency and everything those dependencies depend on. Your own code is usually a small fraction of the total.
Related guides
- node_modules<project>/node_modules
- npm cache~/.npm/_cacache
- pnpm store~/Library/pnpm/store
- Yarn cache~/Library/Caches/Yarn
- Playwright browsers~/Library/Caches/ms-playwright
Uninstall guides
See where your Mac's space went
Diskzy maps your disk, explains System Data and clears what's safe. Free for 72 hours, then $9 once for 2 Macs.
Try Diskzy for free