CleanLoupe
FRAME 01 · DEVELOPER

How to Find and Delete Old node_modules on Mac

To remove old node_modules folders on a Mac, list them with find ~/Projects -name node_modules -type d -prune, check each one's size with du -sh, and delete the folders that belong to projects you are not working on. Nothing is lost: running npm install, yarn or pnpm install in that project rebuilds node_modules from its lockfile. The interactive tool npkill does the same search and lets you delete with a keypress.

This guide shows how to find them, how to pick which to delete, three ways to remove them, and how to stop them creeping back into your backups.

Why node_modules adds up

Every JavaScript or TypeScript project keeps its own copy of its dependencies in <project>/node_modules. A small app can pull in hundreds of packages through transitive dependencies, and build tools, test runners and native binaries for things like image processing or bundlers add more. Multiply that by every tutorial, prototype, client project and cloned repository you have opened over the years and it becomes one of the larger things in your home folder.

The folders do not shrink on their own. Neither npm nor your editor removes node_modules from a project you stopped touching, and Finder hides them inside project folders where you rarely look.

Step 1: find every node_modules folder

Run this with your own code folder in place of ~/Projects. It lists each top-level node_modules with its size, sorted so the biggest are at the bottom:

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

What the parts do:

  • -name node_modules -type d matches folders with that name.
  • -prune stops find from descending into a match, so you do not get thousands of nested node_modules inside node_modules.
  • -exec du -sh {} + measures each match.
  • 2>/dev/null hides permission-denied messages.

If your code is scattered, search your whole home folder. It is slower, and you will want to skip ~/Library, which holds app data and editor extensions that also contain node_modules:

find ~ -path ~/Library -prune -o -name node_modules -type d -prune -print 2>/dev/null

To get a single total for everything found:

find ~/Projects -name node_modules -type d -prune -exec du -sk {} + 2>/dev/null \
  | awk '{s+=$1} END {printf "%.1f GB\n", s/1048576}'

Step 2: decide which to delete

A node_modules folder is worth keeping only if you will run that project soon and do not want to wait for an install. Everything else is a candidate. Two quick signals help:

  • When the project was last touched. ls -ldT <project> shows the folder's modification date, or git -C <project> log -1 --format=%cr shows when the last commit was made.
  • Whether a lockfile exists. If package-lock.json, yarn.lock or pnpm-lock.yaml is in the project, a reinstall will give you the same dependency versions. If there is no lockfile, a reinstall may pull newer versions, which can matter for very old projects.

This command lists node_modules folders whose project has not been modified in 90 days, which is a good starting list:

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

Treat -mtime as a hint, not a verdict: a folder's date changes when its direct contents change, so open the project and check if you are unsure.

Step 3: delete them

In Finder

For a handful of projects, Finder is fine and gives you the Trash as a safety net:

  1. Open the project folder.
  2. Select node_modules and choose File › Move to Trash.
  3. Empty the Trash when you are done.

Emptying a Trash full of node_modules can take a while because of the sheer number of small files. Let it finish.

In Terminal

For one project:

rm -rf ~/Projects/old-app/node_modules

For every match of the 90-day search above, after you have read the list:

find ~/Projects -name node_modules -type d -prune -mtime +90 -exec rm -rf {} + 2>/dev/null

rm -rf is permanent and skips the Trash, so run the -print version first and check what it finds. Never run it on a path you have not looked at.

With npkill

npkill is a small open-source command-line tool built for exactly this job. Run it from the folder where your projects live:

cd ~/Projects
npx npkill

It searches in the background, shows each node_modules with its size and last-modified time, and deletes the highlighted one when you press Space. It is quick and pleasant to use. Like rm -rf, its deletions are permanent.

A note on pnpm and Yarn Plug'n'Play

pnpm links files from a single global store into each project instead of copying them. Deleting a pnpm project's node_modules therefore frees less space than du suggests; the real data sits in the store until you prune it. Yarn 2 and later with Plug'n'Play does not create node_modules at all and keeps zip archives in .yarn/cache inside the project or in a global cache. The global stores and caches are covered in clearing npm, Yarn and pnpm caches.

Keep node_modules out of Time Machine

Backing up node_modules wastes backup space and slows backups, since it is all reproducible. You can exclude a folder from Time Machine:

tmutil addexclusion ~/Projects/my-app/node_modules

The exclusion sticks to the folder, so if you delete and reinstall node_modules you need to add it again. Some developers wrap this in a post-install script.

How CleanLoupe does it

The Developer group in CleanLoupe's Cleanup module has a rule called "Dependencies of old projects". It looks for node_modules, CocoaPods Pods, Dart .dart_tool and Flutter build/ folders in projects that have not been touched for 90 days, in your home folder and on other local disks, and lists them with their sizes. A project counts as touched when its folder, its package.json or lockfile, or its nearest .git/index changes, so opening it in an editor or committing resets the clock. The age is a setting, and Settings › Projects lets you choose which folders it searches. Each folder is labelled Review, or Careful when the project has no lockfile, because a reinstall could then pick newer versions. Neither is preselected: you look at the project names and tick the ones you want gone. Ticked folders move to the Trash and are recorded in History, so Put Back can restore a project's node_modules until you empty the Trash, which saves a reinstall if you change your mind.

It also lists the global npm, pnpm and Yarn Berry caches, labelled Safe.

What it does not do: CleanLoupe does not offer node_modules from projects you used in the last 90 days (or whatever age you set), so for an active project you want to reset, delete the folder yourself. It does not run npm install, pod install or flutter pub get for you, or exclude folders from Time Machine. The My Clutter module deliberately skips node_modules when it looks for large and duplicate files, so they do not clutter those results. The app is free, with no subscription or account.

If you are on Android projects too, clearing the Gradle cache on Mac is the equivalent job for ~/.gradle. For the full list of developer folders, see freeing up disk space on a developer Mac.

FAQ

Is it safe to delete node_modules?

Yes. It contains only installed packages, which your package manager reinstalls from package.json and the lockfile. Keep the lockfile so the reinstall gives you the same versions. Do not delete node_modules while a dev server or build is running in that project.

How do I delete all node_modules folders at once?

Use find with -prune -exec rm -rf {} + on your code folder, as shown above, after checking the list with -print. npkill is an interactive alternative that lets you choose folder by folder. Both delete permanently.

Why is node_modules so big?

Projects depend on packages that depend on other packages, and each project stores its own full copy. Native binaries, TypeScript type packages and build tooling add to it. Measure yours with du -sh node_modules inside a project.

Does deleting node_modules affect other projects?

No. With npm and Yarn 1, each project's node_modules is independent. With pnpm, files are linked from a shared store, but deleting one project's node_modules does not affect the store or other projects.

To review dormant node_modules folders with Put Back as a safety net, download CleanLoupe.

FRAME 10 · KEEP READING