How to clear npm cache on Mac

5 min read

npm clear cache is a hot topic for anyone battling constant npm install failures, or anyone ready to slam their lid, because React just won’t seem to pick up new package versions.

On macOS and Linux, npm stores cached package data in ~/.npm. On a Windows device, it lives in %LocalAppData%\npm-cache. npm 5+ verifies its cache automatically, so you shouldn’t, in honesty, need to clear it all that often, but if it’s become corrupted or stale, then you will probably run into some bigger issues.

Failed installs and even error messages can be really annoying, but the good news is, there’s a pretty simple command that can fix things.

Quick fix for npm clean cache

Any developer out there should have this one-liner to clear the npm cache in their back pocket.

  • npm cache clean --force

Terminal window running npm cache clean --force command on macOS

This is going to work on macOS, Windows, and Linux. Just a quick mention here on the --force flag. It’s there purely because npm 5+ treats the cache as self-healing and dissuades manual deletion unless you explicitly confirm it.

So don’t be surprised if you need to verify it; just use:

  • npm cache verify

You should then see npm check the cache folder and report back that it verified or compressed the cache successfully.

If for any reason this fails, that might mean that the cache isn’t the only problem; I’d try resetting the project install too.

Where does all the npm cache live?

I think this is worth knowing because npm cache problems can look like project problems. I’ve previously wasted time blaming node_modules, even my lockfile, when npm was actually reusing something from its cache. And I also think knowing where the cache lives means you can check its size instead of guessing if it’s really worth clearing.

npm keeps its cache hidden. On macOS and Linux, that folder is usually:

  • ~/.npm

On Windows, npm usually stores the cache here:

  • %LocalAppData%\npm-cache

The cache itself contains all kinds of things, from downloaded package tarballs to metadata, also integrity hashes, and npm’s _cacache content-addressable store. Basically, all the temporary package data npm needs so it doesn’t always have to download everything again.

If you want to check your cache size on macOS or Linux, you can run:

  • du -sh ~/.npm

Terminal du command checking npm cache folder size on Mac

npm cache is temporary storage; it’s fairly safe to remove because npm can rebuild it at a later date.

When should you really clear npm cache on a Mac?

You don’t need to run npm cache clean --force as any kind of daily or routine maintenance. npm’s cache is designed to verify and repair itself, so most installs won’t even benefit from clearing it.

But I do think it’s worth doing when you hit any cache-related errors like EINTEGRITY, missing tarballs, or even repeated install failures for the same dependency. It can also help to clear it after switching Node versions, or changing lockfiles, or simply when React or React Native refuses to pick up a dependency change.

If the main reason you’re clearing it is to try and reclaim some storage space, then I think it’s fair to say ~/.npm is rarely the only offender. You’ll also need to think about Xcode DerivedData, old node_modules folders, Yarn, pnpm, pip, Homebrew, and Docker caches. Becuase they can also hoard tens of gigabytes.

I’ve started using CleanMyMac CLI (public beta), a Terminal-native tool from MacPaw, for exactly this kind of wider cleanup. You can get it installed quickly with: brew install --cask cleanmymac-cli

Once you’re all set up, to rid yourself of all kinds of junk, run:

  • cleanmymac clean dev

CleanMyMac dev cleanup scan results showing Homebrew cache files found

Or if you want to have a closer look and hunt down orphaned node_modules across your project folders, run:

  • cleanmymac purge

Clearing cache for React and React Native

If your particular issue is with React or React Native not picking up dependency changes, what you might find is that npm cache is just one layer in this.

Start by resetting Metro first on React Native, with:

  • npm start -- --reset-cache

If you’re using Watchman, you can clear watches too with this:

  • watchman watch-del-all

And if you want to remove any Metro and Haste caches, you can try running these:

  • rm -rf $TMPDIR/metro-*

  • rm -rf $TMPDIR/haste-*

If you want to do a full reset. First, delete the install output and lockfile. Once that’s done, then clear up npm cache, and finally reinstall:

  • rm -rf node_modules package-lock.json

  • npm cache clean --force

  • npm install

This might feel a bit on the drastic side to some, but it’s the cleanest way to rule out stale local state.

What to do when npm clear cache hasn’t helped?

If npm cache clean --force doesn’t fix the issue, the cache probably wasn’t the real cause, and you’ll need to dig a little deeper.

For EINTEGRITY errors, I would start by checking your registry and lockfile. It’s not that surprising, but a mismatched package-lock.json will keep on repeatedly pulling the wrong integrity hash. Try regenerating it with:

  • rm -rf node_modules package-lock.json

  • npm install

Terminal command removing node_modules and package-lock.json files

If you’ve got any permission errors around ~/.npm, you’ll have to fix folder ownership instead of running npm with sudo; you can try:

  • sudo chown -R $(whoami) ~/.npm

Also check that you’re not mixing up npm’s cache with local project installs. ~/.npm is npm’s global cache folder; node_modules is the local dependency folder inside a specific project. Clearing the cache won’t remove node_modules, and deleting node_modules won’t clear npm’s global cache. They solve different problems.

Do you use any other package managers? If so, then It’s often a good idea to clean those separately, you can use:

  • yarn cache clean

  • pnpm store prune

For those who want to inspect the wider disk picture first before heading straight into removal, run:

  • cleanmymac analyze

CleanMyMac analyze command scanning Photos library derivatives folder

The best npm cleaning cache habit is knowing when not to overdo it. I really only start playing around here when I know something is off; there’s no need to turn this into some kind of daily or weekly maintenance. I find that most of the time, a targeted reset, a clean reinstall, or a quick look at what’s actually taking up space will get you further than randomly deleting things and hoping for the best.