How to Clear npm Cache and node_modules on Mac
Every JavaScript project drops a heavy node_modules folder on your disk, and npm keeps its own global cache on top. Together they can quietly waste tens of gigabytes. Here is how to clean both safely on macOS.
Why node_modules and the npm Cache Get So Big
If you have written any JavaScript on your Mac, you have met the joke: node_modules is the heaviest object in the universe. It is only half a joke. A single modern web project can install hundreds or even thousands of transitive dependencies, and each one arrives with source code, compiled binaries, documentation, and tests. A typical React or Next.js project's node_modules folder routinely lands between 200 MB and 1 GB.
The problem compounds. node_modules is per-project, so if you have twenty repositories cloned on your Mac, you have twenty separate copies of largely overlapping dependencies. Old projects you have not touched in a year still hold their full node_modules on disk. It is not unusual for a developer's home folder to contain 20–50 GB of node_modules across abandoned and active projects combined.
Separately, npm keeps a global cache at ~/.npm (specifically ~/.npm/_cacache). Every package tarball npm downloads is stored here so future installs can be served from disk instead of the network. This cache is content-addressed and grows over time, and while npm is generally good at self-healing it, it can still swell to several gigabytes.
How to Find Where the Space Is Going
Before deleting anything, measure. The du command tells you how much disk a folder is using. To check the npm cache size, open Terminal and run:
du -sh ~/.npm
To find every node_modules folder under your home directory and see how big each one is, use find together with du:
find ~ -name "node_modules" -type d -prune -exec du -sh {} +
The -prune flag stops find from descending into nested node_modules (a node_modules folder can contain more node_modules), which keeps the numbers accurate and the search fast. The output is a list of paths and sizes, so you can immediately spot which old projects are worth cleaning.
To get a single grand total across all of them:
find ~ -name "node_modules" -type d -prune -print0 | xargs -0 du -ch | tail -1
How to Clear the npm Cache
npm gives you a dedicated command for cache maintenance. In recent npm versions the cache is self-repairing, so the "force" flag is required to wipe it outright.
-
1
Check the cache health first. Run
npm cache verify. This validates the cache contents, removes any corrupted or unused entries, and reports how much is stored. Often this alone reclaims space without a full wipe. -
2
Force-clear the entire cache. If you want it all gone, run
npm cache clean --force. The--forceflag is mandatory because npm considers a full wipe unnecessary under normal use. -
3
Confirm the reclaim. Run
du -sh ~/.npmagain to see the cache has shrunk. Your nextnpm installwill rebuild only what it needs.
pnpm store prune and yarn cache clean respectively. pnpm's global content-addressable store at ~/.local/share/pnpm/store (or ~/Library/pnpm) can be large because it holds packages for every project you have ever installed.
How to Delete node_modules Safely
Deleting node_modules is completely safe as long as the project has a valid package.json and lockfile. Everything in node_modules is reproducible: running npm install rebuilds it exactly from your package-lock.json. You are never deleting your own source code — only downloaded dependencies.
Delete a single project's node_modules
From inside a project folder, simply remove it:
rm -rf node_modules
When you next need the project, run npm ci (a clean, lockfile-exact install) or npm install to restore it.
Delete node_modules from old projects in bulk
To reclaim space from projects you are no longer actively developing, you can remove every node_modules folder under a directory at once. Preview first, then delete:
find ~/projects -name "node_modules" -type d -prune -print
Once you have confirmed the list looks right, run the deletion:
find ~/projects -name "node_modules" -type d -prune -exec rm -rf {} +
npm ci restores the exact dependency tree whenever you return to the project.
The one caveat: a very small number of packages run install scripts that fetch platform-specific binaries. These are re-fetched automatically on the next install, so they cost you a little download time but nothing permanent.
Keeping node_modules Bloat Under Control
A few habits keep JavaScript disk usage from spiraling:
- Delete node_modules from dormant projects. If you have not touched a repo in months, remove its node_modules. It restores in seconds when you come back.
- Use pnpm for shared storage. pnpm hard-links packages from a single global store, so identical dependencies across projects are stored on disk only once — a dramatic saving if you juggle many repos.
- Run npm cache verify periodically. This keeps the global cache lean without a destructive full wipe.
- Add node_modules to .gitignore. This is standard, but worth confirming — you never want these folders bloating your repository or backups.
Finding node_modules Bloat with TrimyMac
Hunting down every stale node_modules folder with find works, but it means remembering the flags and reading through raw terminal output. TrimyMac scans your Mac and surfaces the largest space consumers visually — including sprawling node_modules folders and developer caches tucked away in old project directories — so you can see at a glance which forgotten repos are costing you the most.
Instead of manually reconciling du output, you get a clear map of where your disk went and can clear developer clutter across your whole machine in one pass, while leaving your actual source code untouched.
Reclaim Space from Dev Clutter
TrimyMac finds oversized node_modules folders and stale caches across your Mac and clears them safely. Free download for macOS.
Download TrimyMac FreeFrequently Asked Questions
Is it safe to delete node_modules?
Yes, provided the project has a valid package.json and lockfile. node_modules contains only downloaded dependencies, all of which are reproducible. Running npm ci or npm install rebuilds the folder exactly from your package-lock.json. You never lose your own source code by deleting node_modules — only the third-party packages, which come straight back on the next install.
What is the difference between npm cache clean and npm cache verify?
npm cache verify checks the cache for integrity, removes corrupted or unused entries, and reclaims space without wiping everything — it is the recommended routine maintenance command. npm cache clean --force deletes the entire cache. Because the modern npm cache is self-healing, npm requires the --force flag to allow a full wipe, treating it as an unusual action rather than everyday maintenance.
Where is the npm cache stored on Mac?
By default npm stores its cache in ~/.npm in your home folder, with the content-addressed package data under ~/.npm/_cacache. You can confirm the exact location on your machine by running npm config get cache. To check its size, run du -sh ~/.npm. Other package managers use different locations — Yarn and pnpm each maintain their own separate stores.
How do I find all node_modules folders on my Mac?
Use find with du: run find ~ -name "node_modules" -type d -prune -exec du -sh {} +. The -prune flag prevents find from descending into nested node_modules, keeping sizes accurate and the search fast. This gives you a path-and-size list of every dependency folder in your home directory so you can target the biggest, oldest projects first.
Will clearing the npm cache break my projects?
No. The npm cache is a performance optimization, not a source of truth. Your installed dependencies live in each project's node_modules, and the canonical package list lives in your lockfile. Clearing the cache only means the next install downloads packages from the registry again instead of reading them from disk — slightly slower once, with no effect on your projects or their behavior.