How to find and delete every node_modules folder on your Mac

Deleting a node_modules folder is safe as long as the project keeps its package.json and lockfile: npm ci (or pnpm install, yarn install) puts back exactly the same packages. The fastest way to reclaim space is to list every node_modules folder on your Mac by size, then remove the ones in projects you are not working on.

Step 1: list every node_modules folder by size

This finds each top-level node_modules under your code folder and prints its size, biggest last. Change ~/Developer to wherever you keep projects:

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

-prune stops find from descending into each node_modules, so nested packages are not listed separately and the command finishes quickly. To get one total:

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

Step 2: delete the ones you don't need

To remove a single project's dependencies:

rm -rf ~/Developer/old-project/node_modules

To remove every node_modules under a folder at once:

find ~/Developer -name node_modules -type d -prune -exec rm -rf {} +

rm -rf skips the Trash: there is no undo. Run the listing command first and check the paths. Point it at a specific folder rather than your whole home directory, and skip projects you are actively working on so you are not reinstalling in the middle of a task.

If you would rather review the list interactively, npx npkill shows every node_modules under the current folder with its size and lets you delete them one at a time.

Step 3: clear the package manager caches

Removing node_modules does not touch the global caches your package manager keeps. These can grow to several gigabytes on their own:

Package managerCache locationCommand
npm~/.npmnpm cache clean --force
pnpm~/Library/pnpm/storepnpm store prune
YarnShown by yarn cache diryarn cache clean

Old Node.js versions installed with nvm also add up. nvm ls lists them and nvm uninstall <version> removes one.

Bringing a project back

When you return to a project, reinstall from its lockfile:

npm ci          # npm, exact versions from package-lock.json
pnpm install    # pnpm
yarn install    # Yarn

Never delete the lockfile (package-lock.json, pnpm-lock.yaml, yarn.lock) along with node_modules. Without it, a reinstall can resolve newer package versions than the ones the project was built and tested with.

Other JavaScript build output

node_modules is rarely the only folder worth removing. Framework build output — .next, .nuxt, dist, build — is recreated by the next build and is safe to remove from idle projects. The cheat sheet of safe-to-delete folders covers the rest of your stacks.

More guides