JSPM

auto-req-cleaner

1.0.4
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 2
    • Score
      100M100P100Q17984F
    • License MIT

    Automatically removes unused require/import statements in Node.js projects

    Package Exports

    • auto-req-cleaner
    • auto-req-cleaner/dist/index.js

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (auto-req-cleaner) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    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 and import 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

    1. Analyze all JavaScript and TypeScript files in your project:
    auto-req-cleaner
    1. Analyze specific files or directories:
    auto-req-cleaner --pattern "src/**/*.{js,ts}"
    1. Ignore multiple directories:
    auto-req-cleaner --ignore "node_modules/**,dist/**,build/**"
    1. 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:

    1. Make sure your tsconfig.json has the correct decorator settings (shown above)
    2. Update to the latest version of auto-req-cleaner
    3. 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.