How to remove old node_modules folders

A node_modules folder can be reinstalled from the project’s package.json and lockfile, so deleting it for a project you’re not working on is usually fine. Before you do, check that the lockfile is there and that the project doesn’t rely on patched or private packages. Then go one project at a time.

TL;DR

  • Short answer. node_modules can be reinstalled from package.json and the lockfile, so deleting it for a project you’re not working on is usually fine.
  • Check first. Make sure the lockfile is there and the project doesn’t rely on patched or private packages. Go one project at a time.
  • With Mint. Mint finds large node_modules folders in common code folders. One you used in the last 7 days stays in Keep, and an idle one with a lockfile waits in Safe to clean with the command that restores it.

Finding the big folders

List them first and delete nothing yet. In Terminal, find ~/Projects -type d -name node_modules -prune -exec du -sh {} + prints each top-level node_modules folder under ~/Projects with its size. Change the starting folder to wherever you keep your code.

Keep searching and deleting as two separate steps. A one-line find command that also runs rm -rf will delete the wrong thing if the path is off by one folder.

Things to check before you delete

Open the project and look for package-lock.json, yarn.lock or pnpm-lock.yaml. Without a lockfile, a fresh install can pull newer versions than the ones you had, and the project may no longer build the same way.

Also look for patched packages, locally linked packages and dependencies from a private registry you might lose access to. A lockfile only helps if every package in it can still be downloaded. If the project matters, make sure the source is committed or backed up.

We deleted the same node_modules 11 times: what a project in use looks like

Removing one project at a time

Once a project passes those checks, the cleanup itself takes a minute.

  1. Pick a project you’re not working on and confirm it has a lockfile.
  2. Move that project’s node_modules folder to the Trash.
  3. Run the project’s install command, such as npm install, when you next need it.
  4. Check that it builds and its tests pass before you empty the Trash.

What Mint adds

Mint looks for large node_modules folders in common code folders such as ~/github, ~/Projects and ~/Developer. First it checks whether the project is in use, meaning a program is working inside it or something was installed, run or edited there in the last 7 days. A project in use goes to Keep, and the row says why.

An idle folder with a lockfile goes to Safe to clean, already ticked, and its row shows the command that restores it, like npm install. Since a reinstall brings it back, Clean deletes it outright. Without a lockfile, the folder goes to Yours with a note that restoring it may not be possible, and nothing there is ticked for you.

If a node_modules folder comes back after Mint removed it, Mint still lists it but never ticks it, and Auto Care leaves it alone. When you click, Mint checks again and won’t delete the folder if its project was used in the meantime. In the App Store edition, Mint can only look inside folders you’ve given it access to.

How Safe to clean works

Frequently asked questions

Can I delete every node_modules folder?

Not safely. Projects you’re working on need them, and an old project may depend on packages that can’t be downloaded anymore.

Will deleting node_modules delete my source code?

No, as long as you remove only the node_modules folder itself. Double-check the path, especially in Terminal, where a typo can reach the whole project.

What if a project has no lockfile?

A reinstall may then give you different package versions. Keep the folder if you need that project to build exactly as it does today.

Find the node_modules folders you forgot about

Run a scan in Mint and see which projects have a lockfile before you remove anything.

Scan your projects