npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

auto-req-cleaner

v1.0.4

Published

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

Downloads

285

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.