How to Clean node_modules on Mac
Every JavaScript project creates a node_modules folder — and each one can easily reach 200 MB to over 1 GB. Across a dozen projects, that's 10–50 GB of duplicate dependencies sitting on your disk. Here's how to find them, measure the damage, and clean them safely.
How Much Space Are node_modules Using?
Run this in Terminal to find and measure all node_modules folders under your home directory:
find ~ -name "node_modules" -type d -maxdepth 5 -prune 2>/dev/null | while read dir; do
du -sh "$dir" 2>/dev/null
done
Most developers are surprised by the output. It's common to see 15–30 GB spread across projects that haven't been touched in months. Each project gets its own copy of every dependency, even packages that are identical across projects — React, lodash, TypeScript, and hundreds of transitive dependencies are duplicated everywhere.
If you'd rather not use Terminal, MacSweep's SpaceLens gives you a visual breakdown of your disk that makes large folders — including node_modules — immediately visible without needing to remember any commands.
Delete node_modules — The Manual Way
Once you've identified what's there, cleaning is straightforward. This command finds and permanently deletes all node_modules folders up to 5 levels deep:
find ~ -name "node_modules" -type d -maxdepth 5 -prune -exec rm -rf {} +
Warning: This permanently deletes the folders. Files removed this way cannot be recovered from Trash. Before running it, make sure you don't have any unsaved local changes inside a node_modules folder — this is uncommon, but can happen if you've used npm link for local package development.
Restoring is simple: navigate to each project directory and run npm install (or pnpm install / yarn). The lock file — package-lock.json, pnpm-lock.yaml, or yarn.lock — ensures you get the exact same dependency versions back.
Delete node_modules Safely with MacSweep
MacSweep's SpaceLens drills into your disk and surfaces the largest folders without you needing to remember Terminal commands. When you delete through MacSweep, files move to Trash rather than being permanently removed — so if something looks wrong, you can restore from Trash before emptying it. That extra step is worth a lot when you're cleaning gigabytes at once.
Prevent node_modules from Growing Back
Cleaning once helps, but the folders will accumulate again unless you change a few habits:
-
Use pnpm. Unlike npm or Yarn, pnpm uses a content-addressable store on disk. Packages are stored once globally and hard-linked into each project's
node_modules. If ten projects all depend on React 18, only one copy lives on your disk. In practice this saves 50–70% of the space npm would use. -
Clean inactive projects. If you haven't opened a project in several months, its
node_modulesfolder is pure waste. Delete it. When you return to the project, a singlenpm installbrings everything back in under a minute. - Set up a regular cleanup habit. Once a month, run a scan — either the Terminal command above or MacSweep's SpaceLens. The key is catching it before it snowballs. A project you worked on last year has had a year to accumulate; catching it after three months is much less painful.
FAQ
Is it safe to delete node_modules?
Yes. node_modules is fully generated from your package.json and lock file. Running npm install restores the exact same dependency tree. The only risk is if you've manually patched files inside node_modules — a practice worth avoiding for exactly this reason.
Will deleting node_modules break my projects?
Not permanently. Your project won't run until you reinstall (npm install), but no source code or configuration is lost. Dependencies are fully reproducible from the lock file. Treat node_modules the same way you treat build artifacts — generated, not authored.
How often should I clean node_modules?
Monthly is a good baseline. Focus on projects you haven't actively worked on — those node_modules folders are pure waste until you return to the project. If you switch to pnpm, you'll need to clean less often since the global store prevents duplication from the start.
Understand your disk. Clean with confidence.
Start free. Upgrade once when you need deeper cleanup. No subscription.
Download MacSweep