How to clear npm cache and node_modules on Mac

Updated · By the iClearer team

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

ToolClear commandFind the folder
pnpmpnpm store prunepnpm store path
Yarn 1yarn cache cleanyarn cache dir
Yarn 2 and lateryarn cache clean --allyarn config get cacheFolder
Bunbun pm cache rmbun 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.

Scanning is free on every Mac and the free plan includes 500 MB of cleanup. Unlimited cleanup starts at $29.99 per year for one Mac, with a 14-day money-back guarantee. Needs macOS 13 Ventura or later, Intel or Apple silicon.

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