Analyze Node.js project dependencies to find unused, undeclared, and risky packages.
npm install dependency-usage-analyzerA production-ready CLI tool to analyze Node.js project dependencies. It detects unused, undeclared, and risky packages to help you optimize your project's security and performance.
* Unused Dependency Detection: Finds packages listed in package.json but never imported in your code.
* Undeclared Dependency Detection: Finds packages imported in your code but missing from package.json.
* Risk Assessment: Checks for deprecated packages and assigns a basic risk score.
* Size Analysis: Calculates the disk size of each dependency in node_modules.
* Support for JS/TS: Works with JavaScript, TypeScript, JSX, and TSX files.
* Reduce Bundle Size: Identify and remove unused dependencies to make your application lighter and faster.
* Improve Security: Detect deprecated or risky packages that might introduce vulnerabilities.
* Clean Up Codebase: Find undeclared dependencies that might break your build in different environments.
* Save Time: Automate the manual process of checking imports vs package.json.
You can install it globally or run it directly with npx.
``bashInstall globally
npm install -g dependency-usage-analyzer
Step-by-Step Guide
1. Open your terminal and navigate to the root directory of your Node.js project (where your
package.json is located).
`bash
cd /path/to/your/project
`2. Run the analyzer. You can simply run:
`bash
npx dependency-usage-analyzer
`3. View the Report. The tool will scan your files and output a table showing:
* Which packages are used vs unused.
* The size of each package on disk.
* Deprecation warnings or risk status.
4. Take Action:
* Uninstall unused packages:
npm uninstall
* Install undeclared packages: npm install
* Replace deprecated packages: Look for modern alternatives.$3
* Output as JSON (for CI/CD pipelines):
`bash
npx dependency-usage-analyzer --json
`
* Fail on Unused (strict mode for CI):
`bash
npx dependency-usage-analyzer --fail-on-unused
`
* Skip Metadata Fetching (offline mode or faster run):
`bash
npx dependency-usage-analyzer --no-enrich
`$3
`
Starting dependency analysis...
Fetching package metadata...
Calculating package sizes...
┌──────────────────────┬──────┬──────┬──────────┬──────────┐
│ Package │ Type │ Used │ Size │ Status │
├──────────────────────┼──────┼──────┼──────────┼──────────┤
│ react │ Prod │ Yes │ 2.5 MB │ OK │
│ lodash │ Prod │ No │ 4.1 MB │ Unused │
│ request │ Prod │ Yes │ 1.2 MB │ Deprecated │
└──────────────────────┴──────┴──────┴──────────┴──────────┘Summary:
Total Dependencies: 3
Unused: 1
Undeclared: 0
`Development
1. Clone the repository.
2. Install dependencies:
npm install
3. Build the project: npm run build
4. Run the CLI locally: node bin/dua.js`Built by Devesh Kumar Thakur
Connect with me:
* 🐙 GitHub: DeveshKumarThakur
* 💼 LinkedIn: kumardeveshthakur
* 🌐 Linktree: kumardeveshthakur
If you find this SDK helpful, feel free to ⭐ star the repository and connect!
ISC