Developer storage

node_modules disk space: find it and clean it up

Every JavaScript project has its own node_modules folder, so the same dependencies are duplicated across projects. Find them with find . -name node_modules -type d -prune, measure with du -sh, and remove them with rm -rf node_modules. A lockfile plus npm ci or pnpm install rebuilds everything.

Updated 2026-10-27

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 verify checks integrity and garbage-collects; npm cache clean --force wipes it, which is rarely necessary.
  • Yarn Classic stores its cache in ~/Library/Caches/Yarn on macOS. yarn cache clean removes it.
  • pnpm keeps a content-addressable store under ~/Library/pnpm/store. pnpm store prune removes packages no project references, which is safer than deleting the store.
  • nvm downloads Node versions into ~/.nvm/.cache. Remove old Node versions with nvm 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 dedupe to collapse duplicate versions within a project where possible.
  • Run npm prune to remove packages not listed in package.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.

FAQ

Frequently asked questions

Is it safe to delete node_modules?

Yes. node_modules contains installed dependencies, not your source code. The lockfile records exact versions, and npm ci or pnpm install can recreate the folder. The only cost is download and install time.

Why is node_modules so much larger than the dependencies I listed?

Every direct dependency has its own dependencies, and nested or duplicated versions may each get a copy. Development tools, native binaries and type definitions add more. Thousands of small files also consume disk blocks, so the allocated size exceeds the sum of file sizes.

What is the difference between npm cache clean and deleting node_modules?

node_modules is the installed tree used by your project. The npm cache in ~/.npm stores downloaded package archives for reuse across projects. Clearing the cache affects future installs, while deleting node_modules affects only that project.

Does pnpm use less disk space?

Usually. pnpm stores package files once in a content-addressable store and hard-links them into projects, so repeated dependencies across projects share the same blocks. The store still grows, but less than separate npm installs.

Can I delete node_modules from an external drive?

Yes, but the delete is slower over USB and the same rules apply: keep the lockfile, confirm the path, and reinstall when you need the project again. Avoid deleting while a dev server, test runner or editor is using the folder.

Preview first, then clean safely

Maclean breaks down caches, app data, large files and APFS snapshots, and labels the risk of every item before deletion.

Download for Mac→