auto-req-cleaner
v1.0.4
Published
Automatically removes unused require/import statements in Node.js projects
Downloads
285
Maintainers
Readme
auto-req-cleaner
Automatically detect and remove unused require/import statements in Node.js and TypeScript projects. This tool helps keep your codebase clean by identifying and removing unused imports, supporting both CommonJS (require
) and ES6 import syntax.
Features
- Detects unused imports in JavaScript and TypeScript files
- Supports both
require
andimport
statements - Handles destructured imports (e.g.,
const { foo } = require('bar')
) - Supports default exports (e.g.,
const foo = require('bar').default
) - Safe mode with dry-run option
- Fast and efficient analysis
- Flexible file pattern matching
- Full TypeScript decorator support
- Handles both JavaScript (.js) and TypeScript (.ts) files
Installation
You can install the package globally:
npm install -g auto-req-cleaner
Or locally in your project:
npm install --save-dev auto-req-cleaner
Usage
Command Line Interface
If installed globally:
auto-req-cleaner
If installed locally:
npx auto-req-cleaner
Options
-p, --pattern <pattern>
: File pattern to analyze (default: "**/*.{js,ts}")-i, --ignore <patterns>
: Patterns to ignore (default: "node_modules/,dist/")--dry-run
: Show what would be removed without making changes-v, --version
: Show version number
Examples
- Analyze all JavaScript and TypeScript files in your project:
auto-req-cleaner
- Analyze specific files or directories:
auto-req-cleaner --pattern "src/**/*.{js,ts}"
- Ignore multiple directories:
auto-req-cleaner --ignore "node_modules/**,dist/**,build/**"
- Preview changes without removing anything:
auto-req-cleaner --dry-run
Supported Import Types
The tool can handle various import patterns in both JavaScript and TypeScript:
// Regular require
const foo = require('foo');
// Destructured require
const { foo, bar } = require('foo');
// Default export require
const foo = require('foo').default;
// ES6 imports
import foo from 'foo';
import { foo, bar } from 'foo';
// TypeScript decorators
@decorator
class MyClass {
@propertyDecorator
myProperty: string;
@methodDecorator
myMethod() {}
}
TypeScript Support
The tool fully supports TypeScript files including:
- Modern decorator syntax
- Class decorators
- Method decorators
- Property decorators
- Parameter decorators
- All TypeScript-specific import/export syntax
Output
The tool will show:
- Files being analyzed
- Unused imports found in each file
- Summary of cleaning actions
- Success message when no unused imports are found
Example output:
File: src/example.ts
Unused imports:
- require: unused-package
- import: another-unused-package
🧹 Cleaned up 2 unused imports
Safe Mode
Using the --dry-run
option will show you what would be removed without actually making any changes:
auto-req-cleaner --dry-run
Known Limitations
- Only analyzes static imports (dynamic imports like
require(variable)
are not supported) - Does not analyze imports inside comments or strings
- Requires files to be valid JavaScript/TypeScript syntax
- When using TypeScript decorators, ensure your tsconfig.json has the appropriate decorator settings:
{ "compilerOptions": { "experimentalDecorators": true, "emitDecoratorMetadata": true } }
Troubleshooting
If you encounter any issues with TypeScript decorators:
- Make sure your tsconfig.json has the correct decorator settings (shown above)
- Update to the latest version of auto-req-cleaner
- If issues persist, try running with the
--dry-run
option first to verify the analysis
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
Support
If you encounter any issues or have questions, please file an issue on the GitHub repository.