Skip to content

Finding and removing old node_modules folders

Each JavaScript project keeps its own copy of its dependencies. How to find the projects you've left and remove their node_modules without losing anything.

Updated · 3 min read

Every JavaScript project keeps its own copy of the packages it depends on, in a node_modules folder next to its package.json. One folder is often a few hundred megabytes, and it stays in every project you've ever cloned or started, including the ones you stopped working on years ago.

Removing a project's node_modules loses nothing you wrote. It can always be installed again from the project's package.json and lock file.

What it costs to remove one

  • The project won't run or build until its dependencies are installed again. Your package manager's install step does that from package.json and the lock file, downloading anything that isn't in its cache.
  • Changes someone made directly to files inside node_modules, such as a quick patch while debugging, are lost. Patches kept with a tool like patch-package live outside it and come back.
  • Nothing else changes: your code, your Git history and the lock file stay where they are.

That's why it's a sensible thing to remove for projects you're not working on, and a nuisance for the one you're in the middle of.

Finding them in Finder

  1. Open a Finder window on the folder that holds your projects.
  2. Press ⌘F, type node_modules, and choose Name matches: node_modules from the suggestions. Set the search to that folder rather than This Mac.
  3. Choose View › as List, then View › Show View Options and turn on Calculate all sizes to see how large each one is.

Folders inside other node_modules folders show up too; you only need the top one in each project. Leave alone any node_modules inside a lib folder of a Node.js installation (the tools you installed globally live there) and any inside Library.

Removing them

Move a project's node_modules folder to the Trash in Finder. If you change your mind before emptying the Trash, Put Back returns it exactly as it was.

Start with projects whose code you haven't touched in months. The date that matters is when you last changed the project, not when node_modules was last written to.

The package managers' caches

Each package manager also keeps a shared cache of everything it has downloaded, so installs don't fetch the same package twice:

  • npm keeps one in ~/.npm/_cacache and never trims it by itself.
  • pnpm keeps each package version once in its store, ~/Library/pnpm/store, and links it into your projects. Much of the store's size is shared with those projects, so removing it frees less than it shows.
  • Yarn Classic keeps one in ~/Library/Caches/Yarn.

Clearing a cache doesn't break installed projects; the next installs just download again. Yarn's cache can go to the Trash in Finder. npm and pnpm clear theirs with their own tools, which have no button for it; the app runs them for you (below). Don't remove the pnpm store by hand: pnpm's own cleanup removes only the packages no project on your Mac uses.

How the app does it

The scan finds every node_modules next to a package.json in your home folder, along with other build folders such as Rust's target and Python's virtual environments. Select one in the Overview's map or list, and the inspector says what it is and what removing it costs.

A project's node_modules is Usually safe: it comes back, but only when you install again, so on the Cleanup page it never starts checked. What you check goes to the Trash, where you can put it back.

The caches are Safe, so they start checked in Cleanup: Yarn's goes to the Trash, and npm's and pnpm's are cleaned with the tool's own command:

What the app runs, after you review itPRO

npm cache clean --force
npm's package cache
pnpm store prune
Packages no project uses any more

Finding them is free. Moving them to the Trash is PRO.

Safe to delete?

The entries this guide is about, in the words the app shows. Each has its own page with its sources.

  • ~/**/node_modules

    node_modules

    A JavaScript project's installed dependencies, which its package manager can install again.

    Usually safe · Moves it to the Trash

  • ~/.npm/_cacache

    npm cache

    Packages and package information npm has downloaded, kept so it doesn't download them again.

    Safe · Runs npm cache clean --force

  • ~/Library/pnpm/store

    pnpm store

    The shared store where pnpm keeps one copy of every package version your projects use.

    Safe · Runs pnpm store prune

  • ~/Library/Caches/Yarn

    Yarn cache

    Packages downloaded by Yarn 1 (Yarn Classic), kept so installs don't download them again.

    Safe · Moves it to the Trash