If you work on many projects, you know the pattern: a project gets your full attention for a few weeks, then sits untouched for months until a client comes back with a new request. Meanwhile, its node_modules folder sits on your disk taking up hundreds of megabytes — and so does the one next to it, and the one next to that.

Every few months I'd notice my disk filling up and go hunting through my projects folder, checking sizes and deleting node_modules by hand. It's not hard, it's just tedious. So eventually I wrote a small script to do it for me.

What It Does

nmclean finds every node_modules folder under a directory, shows how much space each one takes, and lets you delete them all at once or decide one by one:

$ nmclean ~/projects
Searching for node_modules in /Users/me/projects ...
  1)    648M  /Users/me/projects/api/node_modules
  2)    580M  /Users/me/projects/web/frontend/node_modules
  3)     92M  /Users/me/projects/old-demo/node_modules

Found 3 folders, 1.3G total.

[a] delete all  [o] one by one  [q] quit: o
(1/3) 648M  /Users/me/projects/api/node_modules  delete? [y/n/q]: n
  Skipped.
(2/3) 580M  /Users/me/projects/web/frontend/node_modules  delete? [y/n/q]: y
  Deleted.
(3/3) 92M  /Users/me/projects/old-demo/node_modules  delete? [y/n/q]: y
  Deleted.

Freed 672M.

Nothing is lost — the next time you open one of those projects, npm install brings everything back.

Why a Bash Script?

  1. No dependencies: It doesn't need Node, npm, or anything else installed. It runs on the bash that ships with macOS and on any Linux machine.
  2. Easy to trust: It's a single file you can read in two minutes. For a tool that deletes folders, I think that matters.
  3. Safe by default: It only ever deletes folders named exactly node_modules, skips nested ones, and asks before deleting anything.

Why I Made It Public

At first this lived in my own scripts folder. Then I started moving some of my work to another machine at home, and as a freelancer I sometimes work on a machine provided by a client. Copying scripts around by hand got old fast, so I pushed it to a public repo. Now installing it anywhere is one line:

curl -fsSL https://raw.githubusercontent.com/OfirYaron/nmclean/main/install.sh | bash

This installs it to ~/.local/bin/nmclean. If you prefer to read the code first (you should!), clone the repo and run ./install.sh instead.

Usage

nmclean              # search the current directory
nmclean ~/projects   # search a specific directory
nmclean -n           # dry run: list sizes, delete nothing
nmclean -y           # delete everything found without asking

If your projects always live in one place, set it as the default in your ~/.zshrc or ~/.bashrc:

export NMCLEAN_ROOT="$HOME/projects"

Pro-Tip

Point it at your projects folder, not your entire home directory. Your home directory also holds node_modules folders that aren't project dependencies — globally installed npm packages (under ~/.nvm, for example) and editor extensions. Start with a dry run (-n) to see what it finds before deleting anything.

What's Next

nmclean is the first in what I plan to be a small collection of utilities I use day to day. If you have an idea for an improvement or a feature you'd like to see, open an issue or a pull request — I'd love for it to grow beyond my own needs.

View nmclean on GitHub →