DiskSpace AI

How to find and delete old node_modules folders on Mac

In short

node_modules only holds downloaded packages: npm install (or pnpm, yarn, bun) rebuilds it from the lockfile. Deleting it in projects you haven't touched for a while is safe and often frees tens of gigabytes. The same goes for Python's .venv, Rust's target and build folders like .next.

Why it's safe to delete

node_modules is a copy of the packages listed in package.json. Your code isn't there. If the project has a lockfile (package-lock.json, pnpm-lock.yaml, yarn.lock or bun.lock), installing again brings back exactly the same versions:

npm ci          # or: pnpm install, yarn install, bun install

The cost is time and a download the next time you open the project. That's why it makes sense for projects you haven't touched in weeks, not for the one you're working on today.

Find them all, with their size

Run this from the folder where you keep your code (here ~/Code; change it to yours). It lists every node_modules with its size, the biggest last, without going into the nested ones:

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

If you prefer a list where you pick, npx npkill does the same from the current folder and deletes the one you select with the space bar.

Delete them

Inside the project:

rm -rf node_modules

With pnpm, files in node_modules are links to its shared store, so deleting them frees little by itself. Run pnpm store prune afterwards to remove the packages no project uses any more.

The same idea in other languages

When to be careful

DiskSpace AI does this for you

It analyzes your Mac in minutes, tells you what each folder is and only selects what recreates itself. Your files are kept 7 days in case you change your mind. Free during the beta.

Download the Beta

Folders in this guide

More guides