npm cache clean: How to clear npm cache on Mac

It’s safe to clear npm cache on a Mac with npm cache clean: the cache holds copies of every package npm has downloaded and only speeds up installs. On a Mac used for a few years of JavaScript work it often takes several gigabytes, while your projects and their node_modules stay when you clear it. This guide shows how to check the cache, clear it, and what to do with the Yarn and pnpm caches and old node_modules folders.

MacWashing, System Junk: Xcode junk, user caches, installers and logs found, 22.83 GB ready to be cleaned
  1. Open MacWashing and choose System Junk in the sidebar.
  2. Click Start Scan.
  3. Look through what it found and click Clean. Anything debatable stays unchecked.

Free trial · macOS 14 or later

What the npm cache is

npm stores a copy of every package it downloads in ~/.npm/_cacache, so the next install of the same version doesn’t download it again. npm checks the cache itself and deletes damaged entries automatically, so it never needs clearing to fix installs. The only reason to clear it is to get the space back.

Check the cache with npm cache verify

This command deletes data the cache no longer needs and checks that the rest is intact. It frees some space and keeps the packages you use:

npm cache verify
Terminal after npm cache verify: the number of cached files and their size

How to clear npm cache with npm cache clean --force

This command deletes the whole npm cache. npm asks for --force because clearing is never needed to fix problems, only to reclaim disk space:

npm cache clean --force

Installed packages, global tools and projects stay. The next npm install downloads packages again, so it takes longer and needs the internet.

Clear the Yarn and pnpm caches

Yarn 1 (Classic) keeps a global cache, and yarn cache dir shows where. This command clears it:

yarn cache clean

In Yarn 2 and later, run yarn cache clean --all in a project folder to delete both the shared cache and the project’s cache. If the project keeps its cache in Git (Zero-Installs), use yarn cache clean --mirror instead: it deletes only the shared cache.

pnpm keeps one store for all projects. This command deletes the packages no project on the Mac uses anymore. It doesn’t affect your projects:

pnpm store prune

Old node_modules folders

Each project has its own node_modules, and together they often take more space than the cache. Delete node_modules in projects you don’t work on now: npm install creates it again from package.json and package-lock.json when you return.

Finder, a project folder: the context menu of node_modules with «Move to Trash»

With MacWashing, it’s one click

MacWashing, System Junk: Xcode junk, user caches, installers and logs found, 22.83 GB ready to be cleaned

System Junk in MacWashing clears the npm cache together with the caches of your other apps and tools. It also lists the node_modules folders of projects untouched for 90 days, unchecked, so you decide which to remove.

Free trial · macOS 14 or later

Questions and answers

Will MacWashing delete my own files when it cleans?

No. It removes only what it can match to a known app or a known kind of junk, in places apps leave files; macOS and Apple’s files are never touched. Anything debatable is marked and stays unchecked until you decide.

What about apps that are open?

If an open app holds files being cleaned, MacWashing waits for it to quit or quits it at your request, and opens it again afterwards.

Is it free?

Yes, within the free trial: System Junk and the Uninstaller free up to 25 GB in total, with no account or card needed.

Is it safe to run npm cache clean --force?

Yes. The cache holds copies of packages that npm can download again. Despite the word «force», nothing outside the cache is deleted.

Will clearing the npm cache break my projects?

No. Projects use the packages in their own node_modules. Only the next install of each package is slower, because npm downloads it again.

Why does the npm cache grow back?

npm adds every package version it downloads and doesn’t remove old ones. Run npm cache verify from time to time, or clear the cache when you need the space.