Can I delete node_modules? What happens, and how to clean up
Yes. You can delete node_modules at any time. It contains nothing you wrote. It is built from your package.json and your lockfile, and npm install makes it again. That is why almost every developer deletes it at some point to fix a strange error or to get disk space back.
What deleting it does
rm -rf node_modules
The folder disappears, and with it every installed dependency. Your source code, package.json, the lockfile and your git history are untouched. To get it back:
npm install # or: pnpm install, yarn, bun install
The three cases where it bites
Deleting is safe, but reinstalling is not always identical. Know these before you remove the folder of a project you care about:
- There is no lockfile. Without
package-lock.json,pnpm-lock.yaml,yarn.lockorbun.lock, the reinstall resolves versions from scratch. You may get newer releases than before, and a project that worked last year can fail today. If you have no lockfile, think twice for old projects. - A dependency was removed or changed. Packages can be unpublished or deprecated. An old project may no longer install cleanly.
- You edited files inside
node_modules. Any change made directly in there is lost. Patches that live in the repository, for example frompatch-package, come back when the install runs. Manual edits do not.
Also expect the reinstall to need a network connection, and to take a while for big projects, since native modules compile again.
How much space it frees
Often a lot. A single project’s node_modules is commonly hundreds of megabytes, and several gigabytes for large apps. See the size first:
du -sh node_modules
With pnpm the numbers look different. pnpm keeps one copy of each package in a shared store and links it into every project, so deleting a project’s node_modules frees much less than du suggests. To reclaim space there, prune the store instead. See the pnpm store guide.
Delete it from one project, or from all of them
For one project, run rm -rf node_modules in its folder. To find and delete them across a whole projects folder, read how to delete all node_modules folders at once.
A rule that avoids regret
Delete node_modules freely in projects you are working on: you will reinstall within minutes anyway. Be careful with old projects you may need to build again: check that they have a lockfile first, and know that you are trading disk space for a slower, riskier next start.
The caches behind npm install are a separate topic. See npm cache clean –force and the full developer checklist.
Quick answers
Will deleting node_modules break my project?
No. node_modules is rebuilt from package.json and your lockfile when you run npm install, pnpm install, yarn or bun install. Your code is not in it.
Should I delete package-lock.json too?
No. The lockfile is what makes the reinstall give you the same versions as before. Deleting it can pull newer versions and break things.
Why did deleting node_modules free so little space with pnpm?
pnpm keeps one shared copy of every package in a global store and links it into each project. Removing a project's node_modules frees much less than you expect. The store holds the real files.