Node.js ecosystem disk

Is It Safe to Delete node_modules? A Developer's Guide to What Actually Breaks

If you've ever run du -sh node_modules and seen 500 MB staring back at you from a project you haven't touched in months, you've probably wondered: can I just delete this? The short answer is yes, almost always. node_modules is a fully regenerable build artifact, not source data. This guide explains exactly what the folder contains, when deletion is safe, the rare cases where you need to be careful, and how to get everything back in one command.

What node_modules Actually Is

When you run npm install (or yarn, pnpm install, bun install), your package manager reads package.json and the lockfile (package-lock.json, yarn.lock, pnpm-lock.yaml, or bun.lockb), then downloads every dependency and its transitive dependencies into node_modules/. The folder is a cache of downloaded packages, nothing more.

Your source code lives in src/, your configuration lives in config files, and your dependency manifest lives in package.json. The node_modules folder is derived from those manifests. Deleting it does not change your source code, your git history, or anything that cannot be regenerated.

This is why nearly every well-maintained project's .gitignore file includes a single line:

node_modules/

The community consensus, baked into every generator and template, is that node_modules should never be committed. It should always be regeneratable on demand.

Is It Safe to Delete node_modules? The Definitive Answer

Yes, with one condition: your project must have a valid package.json, and ideally a lockfile. If both exist, deletion is completely safe. You restore everything by running:

npm install

or the equivalent for your package manager:

# Yarn
yarn

# pnpm
pnpm install

# Bun
bun install

Each of these reads the lockfile and reinstalls every package at the exact versions that were pinned when the lockfile was last committed. Your project will be in the same state as before deletion.

What if there is no lockfile?

If you delete node_modules and only have package.json (no lockfile), running npm install will resolve the latest versions matching your semver ranges. In most cases this is fine, but if a dependency released a breaking patch since you last installed, you might see unexpected failures. This is an argument for always committing your lockfile, not an argument against deleting node_modules.

The Rare Exceptions: When You Should Pause Before Deleting

1. Packages that run postinstall scripts

Some packages execute a postinstall script after npm install. Usually this just compiles native bindings (like node-gyp for packages such as bcrypt or sqlite3). These scripts run again automatically when you reinstall, so deletion is still safe. The only wrinkle is if the compilation requires a specific Xcode version or macOS SDK that has since changed. If your project uses native addons and you're on a freshly updated macOS, budget a few extra minutes for the reinstall to recompile.

2. Patched packages (patch-package)

If your project uses patch-package, developer-applied patches to third-party packages are stored in a patches/ directory and re-applied during postinstall. The patches themselves live outside node_modules, so deleting the folder is still safe. Just make sure your package.json has postinstall: patch-package configured so the patches re-apply on reinstall.

3. Monorepos with workspace symlinks

In a pnpm or Yarn Berry workspace, node_modules may contain symlinks to sibling packages within the monorepo. Deletion is still safe as long as you reinstall from the workspace root, not from individual package directories. Running pnpm install or yarn at the root will rebuild the entire symlink graph correctly.

4. Global installs vs. local installs

There are two kinds of node_modules on your Mac. Local ones live inside project directories. Global ones live at a path like /usr/local/lib/node_modules or, if you're using a version manager, somewhere like ~/.nvm/versions/node/v20.x.x/lib/node_modules. Deleting a local project's node_modules has zero effect on global installs and vice versa. Be deliberate about which you're removing.

How Much Space Are node_modules Actually Using?

It adds up fast. A single React app scaffolded with create-react-app or Vite can install 200-400 MB. A Next.js project with a full dependency tree can exceed 1 GB. Developers who work across multiple projects often have dozens of node_modules folders scattered across ~/Developer, ~/Projects, and other directories, together consuming 10-30 GB of space without anyone noticing.

To see how much space all your node_modules folders are consuming at once, run this from your home directory:

find ~ -name "node_modules" -type d -prune -print | xargs du -sh 2>/dev/null | sort -hr | head -20

This prints the 20 largest node_modules folders on your machine, sorted by size. The output is often surprising.

What Happens If node_modules Was Deleted by Accident

If node_modules was deleted by accident, the recovery path is simple: navigate to your project root and run your package manager's install command. As long as package.json and the lockfile are intact (they live in your project root and should be tracked by git), you will get an identical set of packages back.

cd ~/Developer/my-project
npm install

If you're not sure whether the lockfile is intact, check git:

git status

If package-lock.json shows as modified or missing, restore it from the last commit before reinstalling:

git checkout package-lock.json
npm install

This guarantees you get the exact versions that were working before the accident.

A Practical Cleanup Strategy for Developers

Rather than deleting node_modules one project at a time, many developers do a periodic sweep of stale projects. A reasonable approach:

  1. Run the find command above to list all node_modules folders by size.
  2. For each folder, check when the parent project was last used: ls -la ~/Developer/old-project or check git log.
  3. If the project hasn't been touched in several months and has a lockfile, delete the node_modules folder. The project can be restored in under a minute whenever you return to it.
  4. Leave active projects alone; the reinstall overhead is not worth it for code you're touching daily.

To delete a specific folder quickly from Terminal:

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

Or, if you prefer not to type rm -rf, you can drag the folder to Trash from Finder. Both are equivalent.

node_modules vs. Reinstall: What's Different After Reinstalling

The question "delete node_modules vs reinstall" sometimes comes up when people are trying to fix broken installs. Deleting node_modules and running a fresh install is a legitimate debugging step, because it eliminates any partial or corrupted install state. It does not change which versions you get (the lockfile controls that). A clean install is more thorough than running npm install on top of an existing node_modules, because it removes any files left behind by packages that were removed from package.json but not fully cleaned up.

For a truly clean slate, delete both node_modules and the package manager cache:

# npm
rm -rf node_modules
npm cache clean --force
npm install

# pnpm (uses a content-addressable global store, no local cache to clear)
rm -rf node_modules
pnpm install

Automating the Cleanup Across Your Mac

Manually hunting down node_modules folders every few months works, but it requires remembering to do it. If you'd rather see a clear picture of what's consuming space across your entire disk, Crumb's whole-disk map surfaces large directories (including nested node_modules clusters) and lets you review exactly what you're removing before anything is deleted. Its "is this safe to delete?" check explains what each folder is and confirms it's regenerable, which is useful if you're cleaning a machine shared with less experienced team members who might not know a lockfile from a log file. Crumb runs on-device and needs no account.

However you approach the cleanup, the key takeaway is the same: node_modules is designed to be disposable. Delete freely, reinstall confidently.

Reclaim your disk in one click

Crumb audits your whole Mac, tells you what's safe to delete, and frees the space in seconds — private, local, and Apple-notarized.

Download Crumb for macOS

Frequently asked questions

Is it safe to delete node_modules without a backup?
Yes, as long as your project has a package.json and a lockfile (package-lock.json, yarn.lock, etc.). Both of these should be committed to git. Running npm install after deletion will restore every package at the exact versions specified in the lockfile, so no backup of node_modules itself is needed.
Will deleting node_modules affect my git repository?
No. node_modules is listed in .gitignore in virtually every JavaScript project, which means git does not track it. Deleting the folder has zero effect on your git history, branches, or uncommitted changes. Your source code and configuration files are completely separate.
How do I recover if node_modules was deleted by accident?
Navigate to your project root in Terminal and run npm install (or yarn, pnpm install, or bun install depending on your project). If your lockfile is still intact, you will get back exactly the same package versions. If the lockfile was also lost, restore it from git with git checkout package-lock.json before reinstalling.
How do I find all node_modules folders on my Mac to clean them up at once?
Run this command from your home directory: find ~ -name "node_modules" -type d -prune -print | xargs du -sh 2>/dev/null | sort -hr. This lists every node_modules folder sorted by size. You can then decide which ones belong to stale projects and delete those specific folders safely.
Does deleting node_modules and reinstalling fix broken installs?
Often yes. A corrupted or partial install can leave inconsistent files in node_modules that a plain npm install on top of the existing folder will not fix. Deleting node_modules entirely and running a fresh install forces a clean state. Pairing this with npm cache clean --force ensures even cached corrupted packages are flushed out.