What is node_modules, and is it safe to delete?

A node_modules folder holds the packages one JavaScript project depends on, and it is safe to delete because npm, Yarn or pnpm recreates it from your lockfile.

Shiva ChandraMaker of Clean House · · 2 min read
Short answer

A node_modules folder holds the packages that one JavaScript project depends on. It is safe to delete in projects you are not actively running. Your package.json and lockfile stay, and npm install rebuilds the folder. Delete it in old projects first, where it is wasted space.

Path
node_modules folders inside each JavaScript project
What it is
Installed packages for one JavaScript or TypeScript project
Typical size
Hundreds of megabytes per project is common, and Macs with many projects can have tens of gigabytes in total
Made by
npm, Yarn or pnpm (run by you or by AI coding tools)
Safe to delete?
Safe to clear
Comes back?
Yes. Run npm install (or yarn or pnpm install) in the project

What is in this folder?

Every project that uses npm, Yarn or pnpm keeps its own node_modules, so a Mac with dozens of projects holds dozens of copies of the same packages. Copied project folders, git worktrees and AI-generated scaffolds make this worse because each copy keeps its own packages.

Some tools run code from node_modules, so deleting it while a dev server or watcher is running breaks that process.

Is it safe to delete?

Yes. The packages are fully described by package.json and the lockfile, so npm install or npm ci recreates the same versions. The cost is an install and a download, and offline you cannot rebuild it.

Be careful with two cases: folders you edited by hand to patch a package (use a patch tool instead next time), and node_modules folders inside editor extensions, which ship their own and stop working when cleared.

How do I clear it by hand?

  1. Stop the dev server or build in that project.
  2. Delete node_modules in the project folder.
  3. Reinstall only when you return to the project.
cd ~/path/to/project
rm -rf node_modules
npm install
Only delete a project's own node_modules. Do not delete node_modules inside an app's bundle or an editor's extensions folder.

How do I stop it growing?

Use pnpm, which links packages from one shared store, and delete node_modules in projects you finish. Clean House can find them all at once.

How Clean House handles it

Clean House finds node_modules folders across your projects, lists each by project, and marks them as something that comes back when you reinstall. Packages inside a node_modules are treated as part of the project above them. It skips the node_modules inside editor extensions, because clearing those breaks the extension. Files go to the Trash.

Questions

Is it safe to delete node_modules?

Yes. Run npm install and it is recreated from package.json and the lockfile. Stop any dev server first.

Why is node_modules so big?

Each project keeps its own copy of every dependency, including dependencies of dependencies.

Will deleting node_modules delete my code?

No. Your code and package.json are outside node_modules.

Sources

  1. npm Docs: npm install
  2. npm Docs: npm ci