pkg-clean
v1.1.0
Published
Find and remove unused dependencies from your Node.js project
Maintainers
Readme
pkg-clean
Find and remove unused dependencies from your Node.js project
A minimal, fast CLI tool that scans your project for unused npm dependencies. Identifies unused packages, duplicates, and deprecated packages in seconds.
Features
- Scan for unused dependencies - finds packages in package.json that aren't imported anywhere
- Detect duplicates - spots packages listed in both
dependenciesanddevDependencies - Check deprecation status - warns about deprecated packages via NPM registry
- Safe by default - uses dry-run mode, only removes with
--removeflag - Fast - scans large projects in under a second
- Minimal & slick CLI - beautiful colored output with clear formatting
Installation
npm install -g pkg-cleanOr run directly without installing:
npx pkg-cleanUsage
Scan current project
pkg-cleanOutput:
◆ pkg-clean
→ Scanning files...
✓ Found 12 imports
→ Analyzing dependencies...
→ Checking for deprecated packages...
◆ Report
◆ Unused dependencies (3)
● @types/jest
● @types/node
● ts-jest
→ Run with --remove to clean up unused dependenciesRemove unused dependencies
pkg-clean --removeThis will:
- Remove unused packages from your
package.json - Show you which packages were removed
- Suggest running
npm install
Show verbose output
pkg-clean --verboseDisplays detailed information about all imports, dependencies, and analysis results.
Keep specific packages
pkg-clean --keep lodash reactExcludes specified packages from removal (even if unused).
Scan a different directory
pkg-clean /path/to/projectCombine options
pkg-clean /path/to/project --remove --verbose --keep lodashDry-run to preview changes
pkg-clean --remove --verboseOutput (without --remove):
→ Run with --remove to clean up unused dependenciesThis allows you to review what would be removed before applying changes.
Use with npm scripts
Add to your package.json:
{
"scripts": {
"clean:deps": "pkg-clean",
"clean:deps:remove": "pkg-clean --remove",
"clean:deps:check": "pkg-clean --verbose"
}
}Then run:
npm run clean:deps:check # Preview unused dependencies
npm run clean:deps:remove # Remove unused dependenciesCI/CD Integration
Check for unused dependencies in your CI pipeline:
# Fail if unused dependencies are found
pkg-clean --verboseExit code will be non-zero if unused dependencies are detected (useful for CI gates).
Configuration
Create a pkg-clean.config.json file in your project root to set default packages to keep:
{
"keep": ["lodash", "moment"]
}Example configurations:
For a React project:
{
"keep": ["react", "react-dom", "react-router-dom"]
}For a monorepo:
{
"keep": ["@company/shared-utils", "@company/types"]
}For a library with peer dependencies:
{
"keep": ["tslib", "@types/node"]
}How it works
- Scans all
.js,.ts,.jsx,.tsxfiles in your project - Extracts import/require statements using regex patterns
- Compares found imports against package.json entries
- Reports unused, duplicates, and deprecated packages
- Removes from package.json (if
--removeflag is used)
What it detects
✅ Detects
importstatements:import foo from 'bar'require()calls:require('foo')- Scoped packages:
@babel/core - Nested imports:
lodash/map→ detectslodash - Both
dependenciesanddevDependencies - Duplicate packages in both sections
- Deprecated packages from NPM registry
⚠️ Limitations
- Doesn't analyze dynamic imports like
require(variable) - Doesn't check
.json,.html, or config files - Doesn't evaluate webpack/babel configs
- Doesn't track indirect dependencies
Development
# Install dependencies
npm install
# Build TypeScript
npm run build
# Run tests
npm test
# Run tests in watch mode
npm test:watch
# Run locally
node dist/cli.jsContributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT © 2026
