npm-diet
v1.0.0
Published
Automatically remove unused npm dependencies. Lost 500MB in 30 seconds.
Downloads
11
Maintainers
Readme
npm-diet 🍃
Stop paying for unused dependencies.
Automatically detect and remove unused npm dependencies. Reclaim disk space, reduce bundle size, speed up installs.
😱 The Problem
$ du -sh node_modules
847MB node_modules
# Half of those packages? You're not even using them.Every unused dependency:
- 💾 Wastes disk space
- 📦 Increases bundle size
- ⏱️ Slows down npm install
- 🐛 Adds security vulnerabilities
- 😵 Makes package.json unreadable
✨ The Solution
$ npx npm-diet
🔍 npm-diet - Scanning for unused dependencies...
Found 12 unused dependencies:
Dependencies:
- moment
- lodash
- axios
- express
- request
- body-parser
- ...
💾 Space to reclaim: 127.5 MB
Remove these packages? (y/N): y
🗑️ Removing unused dependencies...
✓ Removed 12 dependencies
✓ Removed 3 devDependencies
✨ Freed up 127.5 MB!Your node_modules just lost weight.
🚀 Quick Start
# Scan and preview (dry run)
npx npm-diet --dry-run
# Scan and remove (interactive)
npx npm-diet
# Auto-remove without confirmation
npx npm-diet --auto💡 How It Works
- Scans all
.js,.jsx,.ts,.tsxfiles - Detects
importandrequire()statements - Compares with package.json dependencies
- Finds packages you installed but never imported
- Removes them (after your confirmation)
📊 What Gets Detected
✅ Detects these imports:
// ES6 imports
import React from 'react';
import { useState } from 'react';
import * as axios from 'axios';
// CommonJS requires
const express = require('express');
const { join } = require('path');
// Dynamic imports
const module = await import('lodash');
// Side-effect imports
import 'some-polyfill';✅ Handles scoped packages:
import babel from '@babel/core'; // Detects @babel/core
import parser from '@babel/core/lib/parse'; // Detects @babel/core✅ Ignores relative imports:
import utils from './utils'; // Ignored
import config from '../config'; // Ignored🎯 Usage Modes
1. Dry Run (Preview Only)
npx npm-diet --dry-run
# or
npx npm-diet -dShows what would be removed without actually removing anything.
2. Interactive (Default)
npx npm-dietAsks for confirmation before removing packages.
3. Auto Mode
npx npm-diet --auto
# or
npx npm-diet -yRemoves packages automatically without confirmation.
⚙️ Installation
# Run directly (recommended)
npx npm-diet
# Or install globally
npm install -g npm-diet
diet🔍 What Gets Scanned
File types:
.js- JavaScript.jsx- React JSX.ts- TypeScript.tsx- React TypeScript.mjs- ES Modules.cjs- CommonJS
Excluded directories:
node_modules/dist/build/.next/coverage/
🎨 Example Output
$ npx npm-diet
🔍 npm-diet - Scanning for unused dependencies...
Found 8 unused dependencies:
Dependencies:
- moment (you switched to date-fns)
- request (deprecated anyway)
- lodash (using native methods now)
- body-parser (built into Express 4.16+)
- morgan (removed logging)
DevDependencies:
- @types/node (not using TypeScript anymore)
- eslint-plugin-react (switched to Biome)
- prettier (switched to Biome)
💾 Space to reclaim: 47.3 MB
⚠️ This will uninstall the packages above
Add --dry-run to preview without removing
Add --auto to skip confirmation
Remove these packages? (y/N): y
🗑️ Removing unused dependencies...
✓ Removed 5 dependencies
✓ Removed 3 devDependencies
✨ Freed up 47.3 MB!⚠️ Limitations
May miss:
- ❌ Packages only used in config files (webpack.config.js, .eslintrc.js)
- ❌ Packages required by CLI tools (not imported in code)
- ❌ Packages loaded via string variables (
require(variableName)) - ❌ Peer dependencies
- ❌ Packages used in .gitignored files
Always review the list before confirming removal.
🛡️ Safety Features
- ✅ Dry run mode by default shows what would be removed
- ✅ Interactive confirmation before removing
- ✅ Uses
npm uninstall(safe and reversible) - ✅ Never touches
node_modules/directly - ✅ Only modifies
package.jsonvia npm
If you accidentally remove something: Just npm install package-name it back.
💡 Pro Tips
Run monthly
# Add to your workflow
npx npm-diet --dry-run # Check what's unusedBefore deploys
# Clean up before shipping
npx npm-diet --auto
npm run buildAfter refactors
# Replaced moment with date-fns?
npx npm-diet # Removes moment automaticallyCI/CD integration
# In your CI pipeline
npx npm-diet --dry-run
# Fails if unused deps found (exit code 1)🎯 Use Cases
For Developers:
- ✅ Clean up after removing features
- ✅ Remove packages after switching libraries
- ✅ Audit unused dependencies monthly
For Teams:
- ✅ Reduce node_modules bloat
- ✅ Speed up CI/CD installs
- ✅ Lower hosting costs (smaller deploys)
For Projects:
- ✅ Reduce security surface area
- ✅ Improve bundle size
- ✅ Faster npm install times
📈 Real Results
Project A (React app):
- Before: 847 MB node_modules
- After: 612 MB node_modules
- Saved: 235 MB (28%)
Project B (Express API):
- Before: 312 MB node_modules
- After: 187 MB node_modules
- Saved: 125 MB (40%)🔥 Why This Exists
We've all been there:
- Install package for one feature
- Remove that feature months later
- Forget to uninstall the package
- Repeat 50 times
- 800 MB node_modules folder
npm-diet fixes this automatically.
👤 Author
Daniel Shashko
- GitHub: @danielshashko
- npm: @danielshashko
📄 License
MIT © Daniel Shashko
🍃 Put Your Dependencies on a Diet
npx npm-dietLose the weight. Keep the features.
