JavaScript projects

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 are 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.

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 happily 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.

Removing one project at a time

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

  1. Pick a project you are 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 node_modules folders over 10 MB in common code folders such as ~/github, ~/Projects and ~/Developer, and checks each one for a lockfile. With a lockfile, the row shows the command that restores it, like npm install, and sits under Rebuildable. Without one, it goes under Clutter with a note that restoring it may not be possible.

None of these rows are ticked for you, because the project won’t run until you reinstall. You pick the ones that go and choose Move to Trash or Delete permanently. In the App Store edition, Mint can only look inside folders you have given it access to.

Frequently asked questions

Can I delete every node_modules folder?

Not safely. Projects you are working on need them, and an old project may depend on packages that can no longer be downloaded.

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