import-cleaner
v1.0.1
Published
Tool to scan project files and remove unused import statements
Maintainers
Readme
Import Cleaner
A Node.js tool to scan your project and automatically remove unused import statements.
Installation
# Install globally
npm install -g import-cleaner
# Or locally in your project
npm install --save-dev import-cleanerUsage
Command Line
# Clean current directory
import-cleaner
# Clean specific directory
import-cleaner ./src
# Dry run (preview without making changes)
import-cleaner --dry-run
# Show detailed information about unused imports
import-cleaner --verbose
# Exclude additional directories
import-cleaner --exclude "temp,output,cache"
# Combine options
import-cleaner ./src --dry-run --verboseProgrammatic Usage
const { cleanUnusedImports } = require("import-cleaner");
const stats = cleanUnusedImports("./src", {
extensions: [".js", ".jsx", ".ts", ".tsx"], // file extensions to scan
recursive: true, // scan subdirectories
dryRun: false, // set to true to preview without changes
exclude: ["node_modules", ".git", "dist"], // directories to exclude
verbose: true, // show detailed information
});
console.log(stats);
// {
// filesScanned: 25,
// filesModified: 8,
// totalImportsRemoved: 15,
// unusedImports: [
// {
// file: 'src/components/Button.js',
// imports: [
// { name: 'useState', source: 'react', isDefault: false },
// { name: 'classNames', source: 'classnames', isDefault: true }
// ]
// }
// ]
// }Features
- Scans JavaScript and TypeScript files (
.js,.jsx,.ts,.tsx) - Identifies and removes unused import statements
- Keeps import statements that are actually used
- Works with ES6 named and default imports
- Recursive directory scanning
- Dry run mode to preview changes
- Verbose mode to show detailed information about unused imports
- Automatically excludes
node_modules,.git,dist, andbuilddirectories
How It Works
Import Cleaner analyzes your code using a two-pass approach:
- First pass: Collects all import statements and imported identifiers
- Second pass: Tracks which identifiers are actually used in the code
- Compares the two to identify unused imports
- Removes unused imports while preserving code structure
Limitations
- Currently doesn't detect imports used only in type annotations
- Doesn't track dynamic imports or requires
- May not correctly analyze code with advanced patterns or non-standard usage
License
MIT
