How to clear npm cache and node_modules on Mac
Short answer
To clear the npm cache on a Mac, run npm cache clean --force. The cache lives in ~/.npm and is safe to delete; npm downloads packages again when you next install. Old node_modules folders usually take far more space than the cache. Find them with:
find ~ -name node_modules -type d -prune -exec du -sh {} + 2>/dev/null | sort -h | tail -20
npm cache
See how big it is:
du -sh ~/.npm
Clear it:
npm cache clean --force
npm asks for --force because the cache repairs itself and rarely needs clearing for correctness. Clearing it to get disk space back is fine. To remove only what is no longer needed and check the rest:
npm cache verify
pnpm, Yarn and Bun
| Tool | Clear command | Find the folder |
|---|---|---|
| pnpm | pnpm store prune | pnpm store path |
| Yarn 1 | yarn cache clean | yarn cache dir |
| Yarn 2 and later | yarn cache clean --all | yarn config get cacheFolder |
| Bun | bun pm cache rm | bun pm cache |
pnpm store prune only removes packages that no project on your Mac still uses, so it is safe to run at any time.
Find and delete node_modules
Every project has its own node_modules folder, often several hundred megabytes. Projects you finished long ago still have theirs. List the 20 largest:
find ~ -name node_modules -type d -prune -exec du -sh {} + 2>/dev/null | sort -h | tail -20
Delete one by its path:
rm -rf ~/code/old-project/node_modules
To delete every node_modules folder under one projects folder, move into it first:
cd ~/code
find . -name node_modules -type d -prune -exec rm -rf {} +
Careful: the last command deletes without asking. Run it only inside a folder of your own projects, never in your home folder, because some installed apps and tools keep a node_modules folder they need.
Deleting node_modules is safe for your own projects. Run npm install, pnpm install or yarn in the project to bring it back.
Old Node versions
If you use nvm, each installed Node version is kept in ~/.nvm/versions. List them and remove the ones you no longer need:
nvm ls
nvm uninstall 16
iClearer finds the npm, pnpm and Yarn caches for you
iClearer’s Builders screen shows the npm cache, pnpm store and Yarn cache with their sizes, labelled Safe because your package manager downloads what it needs again. What you clean goes to the Trash. Git repositories, .env files and API tokens can never be selected.
Frequently asked questions
Is it safe to clear the npm cache?
Yes. The npm cache in ~/.npm holds downloaded package files. After you clear it with npm cache clean --force, npm downloads packages again the next time you install them. Your projects are not affected.
Where is the npm cache on a Mac?
In ~/.npm, with the package data in ~/.npm/_cacache. Run npm config get cache to confirm the location on your Mac.
Is it safe to delete node_modules?
Yes, for your own projects. node_modules contains installed dependencies that are listed in package.json. Run npm install, pnpm install or yarn in the project folder to recreate it.
How do I find all node_modules folders on my Mac?
Run find ~ -name node_modules -type d -prune in Terminal. Add -exec du -sh {} + to see the size of each one.
Why does npm cache clean need --force?
npm’s cache checks and repairs itself, so npm treats clearing it as unnecessary for fixing problems and asks you to confirm with --force. Clearing it to free disk space is a normal use.
Related guides
- Free up disk space on a Mac for developersReclaim tens of gigabytes on a developer Mac. Safe commands to clear Xcode DerivedData, npm, pnpm, pip, Homebrew, Docker, simulators and AI model caches.
- How to delete Xcode DerivedDataXcode DerivedData can grow to tens of gigabytes. It is safe to delete. See where it is, three ways to clear it, and what else Xcode leaves on your disk.
- Docker taking up too much space on MacDocker Desktop stores everything in one growing disk image on your Mac. See what is using the space and the safe commands to reclaim it, without losing data.