@hitorisensei/check-unused-deps
v0.9.0
Published
Another tool to analyze the JS/TS codebase for unused dependencies
Readme
@hitorisensei/check-unused-deps
Finds unused dependencies in your package.json. That's it.
What it does
Scans your code with ts-morph (the TypeScript AST thing) and checks what you're actually importing. Works with TypeScript and JavaScript files - all the extensions, doesn't matter.
Picks up:
- Regular imports
- Dynamic imports
- require() calls
- Re-exports
Also checks:
- npm scripts (for stuff like jest, eslint)
- ESLint config files (handles plugin shorthand and all that)
- tsconfig.json and other configs
- @types packages (only marks them used if you use the actual package, except @types/node which is always kept)
- Peer dependencies
Install
npm install -g @hitorisensei/check-unused-depsOr just:
npx @hitorisensei/check-unused-depsHow to use
check-unused-deps # Run it
check-unused-deps /some/path # Or point it somewhere
check-unused-deps --report # Get a detailed text file
check-unused-deps /some/path --report # BothOutput looks like
📦 Reading package.json...
Found 50 dependencies to check
🔍 Scanning codebase for imports...
Found 234 source files to scan
✅ Finished scanning 234 files
... (does its thing checking scripts, configs, etc)
════════════════════════════════════════════════════════════
RESULTS
════════════════════════════════════════════════════════════
⚠️ Found 3 potentially unused dependencies:
📦 Unused in dependencies:
- unused-package-1
🔧 Unused in devDependencies:
- unused-dev-package-1
- unused-dev-package-2
════════════════════════════════════════════════════════════
Total dependencies: 50
Used dependencies: 47
Potentially unused: 3
════════════════════════════════════════════════════════════With --report you get a text file with details on where everything is used.
Heads up
This thing isn't perfect. Might flag stuff that's actually needed if you're doing weird dynamic imports with variables or using some obscure config files I didn't think of.
Also don't blindly delete everything it suggests. Check first. I'm not responsible for your broken builds.
It's pretty good at catching the common stuff though - build tools, eslint plugins, @types packages, etc.
Requirements
- Node 18.12+
- A package.json
Dev stuff
npm install
npm run build
npm testCode's split into modules if you want to poke around:
types.ts- interfacespackage-analyzer.ts- reads package.jsonimport-scanner.ts- scans code with ts-morphconfig-scanner/- checks different config filesdependency-detector.ts- handles special casesreport-generator.ts- makes the reportindex.ts- ties it all together
If you want to add support for more config files or whatever, the code should be straightforward enough to figure out where it goes.
License
MIT
Made because I got tired of manually checking this stuff and other libraries were very slow on large projects.
