Pruneo logoPruneo

How to clean node_modules and npm, pnpm and yarn caches on Mac

Short answer

The biggest win is usually old node_modules folders in projects you no longer open: delete them and run install again when you need the project. Package manager caches are safe to clean with npm cache clean --force, pnpm store prune and yarn cache clean, at the cost of slower installs. Also check for old Node versions in nvm or fnm.

JavaScript projects use space in two places: the node_modules folder inside each project, and the global cache of your package manager. On top of that, version managers like nvm and fnm keep every Node version you ever installed. None of this is dangerous to clean if you know what each part is. This guide goes through them in order, from the biggest and safest to the more specific.

Step 1: find old node_modules folders

Every project has its own node_modules, and projects you stopped using keep theirs forever. This is often the largest amount of space, and the easiest to get back, because any project can rebuild it from package.json and the lock file.

Measure first

Run find only inside the folder where you keep your code (here ~/code, change it to yours). This command only reads:

find ~/code -name node_modules -type d -prune -exec du -sh {} + 2>/dev/null | sort -h

-prune stops find from going inside each node_modules, so it lists only the top folder of each project and runs much faster.

To list only projects whose node_modules hasn’t changed in 90 days:

find ~/code -name node_modules -type d -prune -mtime +90 2>/dev/null

Keep the search inside your code folder. Don’t run it from / or from your whole home folder and delete the results: some apps and global tools have their own node_modules, and also the Node versions managed by nvm live in ~/.nvm.

Delete the ones you don’t need

rm -rf ~/code/old-project/node_modules

Option: npkill

If you prefer a visual list, npkill shows every node_modules below the folder where you run it, with its size and last change:

cd ~/code
npx npkill --dry-run

--dry-run only simulates. Without it, you move with the arrow keys and delete the selected folder with Space. npkill itself warns that some applications need their node_modules to work, and it marks them with a warning sign. Run it from your code folder, not from the root of the disk.

Step 2: the npm cache

npm keeps every package it downloads in a content cache, by default in ~/.npm on macOS (the data is in ~/.npm/_cacache). You can check the location with:

npm config get cache
du -sh "$(npm config get cache)"

Lower risk: verify

npm cache verify

According to the npm documentation, this garbage collects data that is no longer needed and checks the integrity of the cache. It is safe to run any time.

Clean the whole cache

npm says the cache is self-healing and that it should never be necessary to clear it for any reason other than reclaiming disk space. If you want the space, clean requires --force:

npm cache clean --force

Step 3: the pnpm store

pnpm keeps one copy of each package version in a shared store and links it into your projects. On macOS the default is ~/Library/pnpm/store, unless you set PNPM_HOME or XDG_DATA_HOME (pnpm store settings). To see yours:

pnpm store path
du -sh "$(pnpm store path)"

To remove packages that no project uses anymore:

pnpm store prune

The pnpm docs say running it is not harmful and has no side effects on your projects, and suggest running it from time to time, but not too often. A package that an old project’s node_modules still links to counts as used, so delete old node_modules first (Step 1) and prune after.

Step 4: the yarn cache

The command depends on your yarn version. Run yarn --version to check.

Yarn Classic (1.x)

yarn cache dir

shows where the global cache is (Yarn Classic docs). yarn cache list shows the packages in it.

yarn cache clean

Yarn Berry (2.x and later)

By default, modern Yarn stores the cache in a global folder inside ~/.yarn/berry (the enableGlobalCache setting is true by default). Some projects keep a local cache in .yarn/cache instead, often to commit it (.yarnrc.yml settings).

yarn cache clean --all

According to the yarn cache clean reference, --mirror cleans the global cache and --all cleans both the global cache and the current project’s local cache. Without options, it cleans only the local cache.

Step 5: old Node versions in nvm or fnm

Each Node version takes space, and its global packages come with it. Version managers never remove them on their own.

nvm

nvm ls
du -sh ~/.nvm/versions/node/* 2>/dev/null

nvm ls lists the installed versions (nvm README). To remove one you don’t use:

nvm uninstall 18
nvm cache clear

nvm cache clear deletes cached Node downloads.

fnm

fnm list
fnm uninstall 18

fnm list shows the installed versions and fnm uninstall removes one (fnm commands). The same warning applies: you can install it again with fnm install.

A simple routine

Once a month, run the find from Step 1 and delete node_modules in projects you haven’t touched in months, then run pnpm store prune if you use pnpm. Clean the npm or yarn cache only when you really need the space. If you also build Apple apps or use containers, read how to delete Xcode DerivedData safely and Docker prune on Mac. For the rest of your disk, see how to free up space on Mac or download Pruneo for free.

FAQ

Is it safe to delete node_modules?

Yes, inside your own projects. The folder is rebuilt from package.json and the lock file with npm install, pnpm install or yarn install. Don't delete node_modules folders that belong to installed apps or global tools.

Should I run npm cache clean --force?

Only to get disk space back. npm says the cache is self-healing and that clearing it should never be needed for any other reason. Use npm cache verify first.

Does pnpm store prune break my projects?

No. pnpm says it only removes packages no project references and has no side effects on your projects. A later install may download a removed package again.

Where is the pnpm store on Mac?

By default it is in ~/Library/pnpm/store, unless PNPM_HOME or XDG_DATA_HOME is set. Run pnpm store path to see the exact location.

Try it with Pruneo

Free for macOS 14 or later. Anything it cleans can be restored from quarantine.

Pruneo is for Mac

Send yourself the link and download it on your Mac. Free, no account needed.

Email it to me
Download anyway