Is it safe to delete node_modules on Mac?

Safenode_modules

Yes. node_modules rebuilds from package.json and the lockfile. How to find every copy, measure them, delete them safely and get a project running again.

Updated 16 November 2026 · 3 min read

Verdict: Safe. Deleting a node_modules folder is safe, because it only holds packages installed from the project's package.json and lockfile, and one install command puts it back. Stop anything running in the project first, then delete the folder and reinstall when you next need the project.

What lives in node_modules

When you run npm install, npm puts every package the project needs, and every package those need, into ./node_modules in the project folder. npm's folders documentation calls this a local install, and it is the default. Yarn Classic works the same way.

That is why the folders get so big. Each project gets its own full copy of every dependency, so ten projects built on the same framework store it ten times. Some packages also download platform-specific binaries or browsers during install.

pnpm is different. It keeps one copy of each package version in a shared store and links or clones the files into each project, so a pnpm project's node_modules takes up less room of its own.

What happens if you delete it

  • Rebuilds by itself: everything that came from the registry. npm ci installs exactly the versions in package-lock.json; pnpm install, yarn install and bun install do the same from their lockfiles.
  • Changes if there is no lockfile: without one, a reinstall may pick up newer versions that behave differently. Check for package-lock.json, pnpm-lock.yaml, yarn.lock, bun.lock or bun.lockb first.
  • Lost for good: any edit you made by hand inside node_modules. Tools such as patch-package keep patches outside the folder, so those survive.

Your source code, package.json and lockfile are outside node_modules and are never affected.

How to check its size safely

For one project, from inside its folder:

du -sh node_modules

To find every node_modules under a folder and list them by size, replace ~/Projects with wherever you keep your code:

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

Both commands only read. The -prune part stops find from searching inside each node_modules, which can hold thousands of folders. If you point it at your whole home folder, macOS may ask whether Terminal can access Desktop, Documents or Downloads.

In Finder, select a node_modules folder and choose File > Get Info. The size can take a moment to appear.

The safest way to get the space back

  1. Stop dev servers, watchers and test runners for the project.

  2. Check there is a lockfile next to package.json.

  3. In Finder, select node_modules and press Command-Delete to move it to the Bin. In Terminal, macOS 14 Sonoma and later include a trash command:

    cd ~/Projects/my-app
    trash node_modules
    

    This moves the folder to the Bin, so you can still put it back.

  4. Empty the Bin. Apple's Free up storage space on Mac notes that the space only returns once you do. With so many small files, emptying can take a while.

  5. When you next work on the project, run npm ci (or pnpm install, yarn install, bun install).

If you prefer to skip the Bin, rm -rf node_modules from inside the project does it in one step. Warning: rm -rf deletes immediately with no undo. Run pwd first to check you are in the right project.

For pnpm projects, deleting node_modules frees less than you expect. pnpm store prune removes packages no project uses from the shared store, as covered on the ~/Library/pnpm page.

What never to touch

  • node_modules in a project that is running right now. Deleting files under a running process causes confusing errors.
  • package.json and the lockfile. They are what lets you rebuild.
  • Global installs. Packages installed with npm install -g live under npm's global prefix, not in a project. Uninstall them with npm uninstall -g instead.
  • .env files and local databases that sit near build folders. They are often ignored by Git, but they are not build output.

Our guide to deleting node_modules and other build folders safely covers bulk deletes and folders like .next and target. Package downloads live separately in the npm cache, which is safe to clear too.

How Headroom helps

Headroom's Clean scan looks for node_modules in your code folders, such as Downloads, Documents, Desktop, Developer, Projects and Code. It only lists one when package.json sits beside it, no file in the project apart from build output has changed for 14 days, and no process is running inside. Each line says "Run npm install before you next start it." They are ticked and deleted for good, because they rebuild. The scan is free; the $19 one-time licence clears what you tick. See the cleaner or download Headroom.

Questions

Will deleting node_modules break my project?

Not for good. The project will not run until you reinstall, but npm ci, pnpm install, yarn install or bun install rebuilds node_modules from package.json and the lockfile.

How do I reinstall node_modules after deleting it?

In the project folder, run npm ci if there is a package-lock.json, or the install command of the package manager the project uses. You need a network connection, or a package cache that already holds the packages.

Can I delete node_modules in projects I am not using?

Yes, and that is usually where most of the space is, because each idle project keeps its own copy of every dependency. Reinstall when you next open the project.

Why didn't deleting a pnpm project's node_modules free much space?

pnpm keeps each package once in a shared store and links or clones files into each project. Most of the space lives in the store, which you clean with pnpm store prune.