How to Clear the npm Cache on a Mac, and When You Should
npm keeps a copy of every package it has ever downloaded, and every project keeps its own node_modules folder. Both are safe to remove and both come back when needed. Here is how to clear them and what it costs.
To clear the npm cache, run npm cache clean --force in Terminal. It is safe: the cache holds only downloaded packages, and npm downloads them again when a project needs them. The larger saving is usually old node_modules folders in projects you no longer work on.
Where the npm cache is and how big it is
On a Mac the cache is at ~/.npm/_cacache unless you changed it. To confirm the location, run npm config get cache. To see its size, run du -sh ~/.npm. A few gigabytes is normal after a year or two of use.
How to clear it
- Open Terminal.
- Optional: run
npm cache verify. This checks the cache and removes data that is no longer needed, without emptying it. - To empty it completely, run
npm cache clean --force.
The --force flag is required because, as the npm documentation explains, the cache repairs itself and should not need cleaning for any reason other than reclaiming disk space.
What it costs you
Nothing breaks. The next npm install in any project downloads its packages again, so it is slower and needs a network connection. Projects that are already installed keep working, because they run from their own node_modules folder, not from the cache.
node_modules is usually the bigger saving
Each project has its own node_modules folder, often several hundred megabytes, and old projects keep theirs forever. Deleting one is safe as long as the project has a package.json, and ideally a lock file, beside it: running npm install (or npm ci to match the lock file exactly) in that folder restores it.
To list them with their sizes without deleting anything, run:
find ~ -name node_modules -type d -prune -exec du -sh {} +
Remove the ones in projects you are not working on, and leave current projects alone.
Yarn and pnpm
Yarn and pnpm keep their own stores. yarn cache clean empties Yarn’s cache, and pnpm store prune removes packages in the pnpm store that no project uses any more. The cost is the same as with npm: packages are downloaded again when needed.
If you also build for Apple platforms, see whether it is safe to delete Xcode DerivedData.
Check the size before and after
Measure first, so you know whether clearing is worth it. These commands only read sizes:
du -sh ~/.npmshows the whole npm folder, including the cache and logs.du -sh ~/.npm/_cacache ~/.npm/_npx 2>/dev/nullsplits it into the package cache and the npx cache.
Run the first command again after cleaning. If the number barely moved, the space is elsewhere, usually in node_modules folders or old Node versions, both covered below.
Other Node caches that grow quietly
- The npx cache (
~/.npm/_npx). Every package you run withnpxis kept here, andnpm cache cleandoes not empty it. Deleting the_npxfolder is safe; npx downloads a package again the next time you run it. - Old Node versions. If you use nvm, each installed version lives in
~/.nvm/versions/nodewith its own global packages. Runnvm lsto list them andnvm uninstall 16.20.2(with your version number) to remove one you no longer use. - Global packages.
npm ls -g --depth=0lists them. Remove ones you no longer use withnpm uninstall -gfollowed by the package name. - Yarn and pnpm stores.
yarn cache dirandpnpm store pathprint where they keep packages, so you can measure them withdu -shbefore cleaning.
Quick answers
Is it safe to delete the ~/.npm folder?
Yes. It holds the package cache and logs, and npm recreates it. Clearing it with the npm command is tidier than deleting the folder by hand.
Will clearing the npm cache fix install errors?
Rarely. npm checks cached data before using it, so a damaged cache is seldom the cause. Deleting node_modules and installing again fixes more problems.
Does deleting node_modules lose any of my work?
No, provided you have not edited files inside it. Your own code, package.json and lock file are outside the folder.