Key points
- node_modules is regenerable; package-lock.json, yarn.lock or pnpm-lock.yaml is the file that must be kept.
- Search with find -name node_modules -type d -prune so the command does not descend into every dependency tree.
- Measure with du -sh, not ls, because thousands of small files consume more blocks than their nominal size.
- Remove inactive projects first, then reinstall with npm ci, yarn install --frozen-lockfile or pnpm install --frozen-lockfile.
- Package manager caches in ~/.npm, ~/Library/Caches/Yarn and ~/Library/pnpm/store are separate and can also be pruned.
node_modules grows because each project installs its own copy of every dependency, including dependencies of dependencies. The folders are safe to delete, but expensive to rebuild on a slow connection. Find all of them first, measure the total, and remove the projects you are not actively working on.
Why node_modules gets so large
A typical dependency tree is much larger than the list in package.json. Each package brings its own dependencies, and npm may install multiple versions of the same package at different levels. Type definitions, test runners, bundlers and native binaries add more. A project with a dozen direct dependencies can still have hundreds of packages on disk.
The file count matters as much as the total size. A dependency tree can contain thousands of small files, and each file consumes at least one disk block. That is why du -sh often reports a larger number than adding up the file sizes would suggest.
Find node_modules folders
Search from the directory where you keep projects:
find ~/Projects -name node_modules -type d -prune -print
-prune tells find not to descend into a matched directory. Without it, find would walk through every package inside every node_modules folder, which is slow and produces nested matches you do not need.
Count them:
find ~/Projects -name node_modules -type d -prune | wc -l
Measure and rank them:
find ~/Projects -name node_modules -type d -prune -exec du -sh {} + | sort -hr | head -20
If your projects are spread across the home folder, search ~ instead, but avoid scanning / or external volumes unnecessarily. Add 2>/dev/null to suppress permission errors.
Measure before deleting
Inside a project:
du -sh node_modules
du -sh .
The first number is the dependency tree; the second shows how much of the project is dependencies. On a project with a large build output, node_modules may still be the biggest folder. du measures allocated blocks, which is the number that matters for free space.
Delete and rebuild
Check that a lockfile exists before removing anything:
ls package-lock.json yarn.lock pnpm-lock.yaml
Then delete:
rm -rf node_modules
This is permanent; rm does not use the Trash. Run pwd first and make sure you are in the project directory, not in your home folder or /. If you want an undo path, move the folder to the Trash from Finder instead, though deleting thousands of files through the Trash is slower.
Reinstall with the lockfile:
npm ci
yarn install --frozen-lockfile
pnpm install --frozen-lockfile
npm ci removes the existing node_modules and installs exactly what the lockfile specifies. It is usually faster and more predictable than npm install. If a project has no lockfile, commit one before deleting; otherwise the next install may resolve newer versions.
Clean package manager caches
Project folders are only part of the picture. Package managers also keep downloaded archives:
npm config get cache
npm cache verify
yarn cache dir
yarn cache clean
pnpm store path
pnpm store prune
- npm stores downloads in
~/.npm.npm cache verifychecks integrity and garbage-collects;npm cache clean --forcewipes it, which is rarely necessary. - Yarn Classic stores its cache in
~/Library/Caches/Yarnon macOS.yarn cache cleanremoves it. - pnpm keeps a content-addressable store under
~/Library/pnpm/store.pnpm store pruneremoves packages no project references, which is safer than deleting the store. - nvm downloads Node versions into
~/.nvm/.cache. Remove old Node versions withnvm uninstall <version>rather than deleting files.
Global packages are separate from project dependencies. List them with npm ls -g --depth=0 and remove unused ones with npm uninstall -g <package>.
Prevent the duplication from returning
- Use pnpm, which hard-links packages from one store instead of copying them into every project.
- Use workspaces so related packages share a hoisted dependency tree.
- Run
npm dedupeto collapse duplicate versions within a project where possible. - Run
npm pruneto remove packages not listed inpackage.json. - Remove unused dependencies instead of leaving them for a future cleanup.
- Standardize on one or two Node versions and uninstall the rest.
Yarn Plug’n’Play avoids node_modules entirely, but it changes how tools resolve packages and is not a drop-in fix for every project.
How Maclean handles node_modules
Maclean’s developer-cache scan searches common project locations for node_modules, measures each folder, and includes npm, pnpm and Yarn caches. Every item is labeled as regenerable, and project search stops at a few directory levels rather than walking the entire disk.
maclean scan --tab dev-cache
maclean clean --tab dev-cache --dry-run
Developer-cache tabs delete permanently in the CLI, so review the dry run before confirming. Large files and duplicate files move to the Trash and can be restored with maclean restore <id>. The app adds a visual review and a 14-day trial; the CLI is free and open source under Apache-2.0.
For related developer storage, see Docker disk space on Mac and where the Hugging Face cache lives.
Bottom line
node_modules is duplicated by design, so the space adds up across projects. Find every folder with find -prune, delete the ones for inactive projects, and reinstall from the lockfile when you return. Clean the package manager caches separately, and prefer pnpm or workspaces when you want the savings to last.