Stop deleting node_modules. Find out why it broke.
cleaninstall-node diagnoses broken installs and applies the smallest fix that works,
warns you the moment a git pull changes your dependencies, and cleans safely
across monorepos when you really do want a fresh start.
$ npx cleaninstall-node doctor 🩺 cleaninstall-node doctor — ~/code/app (npm) ✖ node_modules is out of sync with package-lock.json (1 difference) ms: installed 2.1.3, lockfile 2.1.2 Usually happens after git pull / switching branches without reinstalling. ✖ 1 native module was built for a different Node version better-sqlite3: built for ABI 115, this Node needs 127 ✔ Node v22.11.0 matches .nvmrc (22) ✔ All 42 direct dependencies installed and in range 2 errors, 0 warnings. Smallest fix (run with --fix to apply): → npm install → npm rebuild better-sqlite3
$ git pull Updating 3bd10e1..0ee145e Fast-forward package-lock.json | 6 +++--- package.json | 2 +- 📦 cleaninstall-node: dependencies changed (package-lock.json, package.json) ✖ node_modules is out of sync with package-lock.json (1 difference) → run: npm install (or: npx cleaninstall-node doctor --fix) $ git checkout feature/docs Switched to branch 'feature/docs' # only README changed — the hook stays silent
$ npx cleaninstall-node --install Will remove: apps/web/.next/ (412.3 MB) apps/web/node_modules/ (286.0 MB) node_modules/ (1.21 GB) packages/ui/dist/ (3.4 MB) Delete 4 item(s), 1.90 GB? (y/N) y ✅ Cleanup completed successfully! 📊 Summary: - Items removed: 4 - Space freed: 1.90 GB - Time: 2.41s 📦 Running pnpm install...
The fix is rarely “delete everything”
Most broken installs have one specific cause. cleaninstall-node finds it, tells you, and fixes only that, so you can skip the five-minute reinstall.
doctor
Checks your Node version, lockfile sync, dependency ranges, native modules and package manager, then suggests the smallest fix. --fix applies it.
git hook
After a pull, branch switch or rebase changes your dependencies, it warns you (or runs the install) before you hit a confusing error.
clean
Workspace-aware removal of node_modules, build output and caches. It shows sizes, asks once, and keeps your lockfiles unless you say otherwise.
Diagnose first. Fix the smallest thing.
doctor looks at what's actually installed, compares it with what your project asks for, and suggests the least disruptive repair.
Rebuild one package
npm rebuild better-sqlite3
A native module was built for another Node version or for another OS/CPU (a Docker volume, Rosetta).
Install
npm install
node_modules doesn't match the lockfile, or a dependency is missing or out of range.
Clean reinstall
rm node_modules + install
Only when node_modules was written by a different package manager than the project uses.
On the wrong Node version, --fix does nothing and tells you to switch first, so nothing gets rebuilt for the wrong Node.
| Check | Catches | Fix |
|---|---|---|
| Node version | Running Node vs .nvmrc, .node-version, .tool-versions, Volta, engines.node | nvm use |
| Lockfile sync | node_modules doesn't match the lockfile, e.g. after a pull (npm, pnpm, yarn 1) | install |
| Direct dependencies | A dependency is missing or installed outside its range, including in workspaces | install |
| Native modules | A .node addon was built for another Node ABI or OS/CPU. Each is loaded in a separate process | rebuild <pkg> |
| Installer | node_modules was written by npm, but the project uses pnpm (or similar) | clean reinstall |
| Lockfiles | More than one package manager's lockfile, a mismatch with packageManager, or no lockfile | by hand |
| pnpm builds | pnpm 10 skipped a package's build scripts | pnpm approve-builds |
--json gives machine-readable output, and the exit code is 1 when there are errors, so it works in CI too.
Know when to reinstall, right when it happens
Someone bumps a dependency and you pull. Nobody reinstalls, and an hour later the build breaks in a confusing way. The hook catches the moment the lockfile changes.
- Silent unless it matters. If no dependency file changed, or node_modules already matches, you see nothing.
- Warn or auto-install.
--mode installruns the install or a targeted rebuild, and never deletes node_modules. - Team-wide with husky. It writes to
.husky/so you can commit it, and keeps your existing hooks. - Never breaks git. It's skipped in CI, and
CLEANINSTALL_SKIP_HOOK=1skips it once.
npx cleaninstall-node hook install
$ git checkout release/2.4 Switched to branch 'release/2.4' 📦 cleaninstall-node: dependencies changed (pnpm-lock.yaml) ✖ node_modules is out of sync with pnpm-lock.yaml (3 differences) → run: pnpm install (or: npx cleaninstall-node doctor --fix)
When you really want a fresh start, do it safely
Finds every workspace from package.json or pnpm-workspace.yaml and cleans it in one go.
- Asks once, with the full list and sizes. Use
-yin scripts; CI and pipes don't prompt. - Keeps lockfiles by default, so a “clean install” doesn't quietly upgrade your dependencies. Use
--lockfilesfor a full reset. - Knows framework output:
.next,.nuxt,.svelte-kit,.astro,.turbo,.vite,coverageand more. - Reinstalls with the right tool, detected from
packageManageror your lockfile. - Dry run and interactive modes, plus config in
package.jsonor.cleaninstallnoderc.
$ npx cleaninstall-node --dry-run [Dry Run] Would remove: dist/ (1.2 MB) node_modules/ (293.0 MB) ✅ Cleanup (Dry Run) completed successfully! 📊 Summary: - Items would be removed: 2 - Space to be freed: 294.2 MB
Get cleaninstall-node
Run it once with no install, add it to your project so the git hook works for everyone, or install it globally. Requires Node.js 20+.
With cleaninstall-node in devDependencies, add a prepare script. Everyone gets the git hook on their next install.
{
"scripts": {
"prepare": "cleaninstall-node hook install",
"doctor": "cleaninstall-node doctor",
"fresh": "cleaninstall-node -y --install"
}
}Prefer a file? Get the package tarball for the latest version, or browse every release.
Upgrading from 2.x
- Lockfiles are kept by default. Pass
--lockfilesto remove them. - The CLI asks once before deleting in a terminal. Add
-yto scripts; CI and pipes don't prompt. - Node.js 20+ is required.
Full list in the changelog.