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 🙏

© 2026 – Pkg Stats / Ryan Hefner

dead-code-finder

v1.0.6

Published

A CLI tool to find unused components, functions, variables, and files in React and Next.js projects.

Readme

Dead Code Finder

A CLI tool to help developers identify and remove unused components, functions, variables, and files in their React and Next.js projects. This helps in reducing bundle size, improving performance, and maintaining a cleaner codebase.

Features

  • Comprehensive Scan: Detects unused:
    • React Components
    • JavaScript/TypeScript Functions
    • Variables
    • Files
    • Imports
  • Dual Analysis Modes:
    • AST-based Analysis (Default): More accurate parsing using Abstract Syntax Trees for precise code understanding
    • Regex-based Analysis: Faster analysis using pattern matching for quick scans
  • React & Next.js Aware: Specifically designed to understand common patterns in React and Next.js applications, including App Router and Page Router conventions
  • TypeScript Support: Fully compatible with TypeScript projects (.ts, .tsx files) with proper AST parsing
  • Configurable: Customize source directories, ignore patterns, and analysis mode via CLI or configuration file
  • Detailed Report: Provides a summary of findings and a list of identified dead code with line numbers, including potential file size savings
  • Production Ready: Successfully tested and working with real-world React/Next.js/TypeScript codebases

Installation

You can install the package globally using npm or yarn:

npm install -g dead-code-finder
# OR
yarn global add dead-code-finder

Usage

Once installed, you can run the find-dead-code command in your project's root directory.

find-dead-code scan

Options

You can customize the scan behavior using the following options:

  • -s, --src <directory>: Specify the source directory to scan (overrides config file).
    • Example: find-dead-code scan --src ./app
  • -i, --ignore <patterns...>: Provide a list of glob patterns to ignore (overrides config file).
    • Example: find-dead-code scan --ignore "**/lib/**" "**/utils/**"
  • -c, --config <path>: Path to configuration file (default: deadcoderc.json).
    • Example: find-dead-code scan --config ./custom-config.json
  • -m, --mode <mode>: Analysis mode: ast (default) or regex.
    • Example: find-dead-code scan --mode regex for faster analysis

Full Example:

find-dead-code scan --src ./src --ignore "**/api/**" "**/types/**"

Configuration File

For larger projects or more complex setups, you can use a configuration file instead of CLI arguments. The tool will automatically look for deadcoderc.json in your project root.

Create a sample configuration:

find-dead-code init

Example configuration file (deadcoderc.json):

{
  "srcDir": "src",
  "ignorePatterns": [
    "**/node_modules/**",
    "**/.next/**",
    "**/dist/**",
    "**/build/**",
    "**/*.test.*",
    "**/*.spec.*",
    "**/__tests__/**",
    "**/__mocks__/**",
    "**/lib/**",
    "**/utils/**"
  ],
  "analysisMode": "ast"
}

Configuration precedence:

  1. CLI arguments (highest priority)
  2. Configuration file
  3. Default values (lowest priority)

How it Works

The tool supports two analysis modes:

AST-based Analysis (Default)

Uses Abstract Syntax Tree parsing for precise code understanding:

  1. Parse Code: Each file is parsed into an AST using TypeScript parser (for .ts/.tsx) or Babel parser (for .js/.jsx)
  2. Extract Definitions: Traverse the AST to find all variable declarations, function declarations, component definitions, and imports
  3. Track Usage: Identify all references, function calls, JSX usage, property access, and other usage patterns
  4. Cross-reference: Compare definitions against usage patterns across the entire codebase
  5. Accurate Results: Provides the most accurate dead code detection with minimal false positives

Regex-based Analysis

Uses pattern matching for faster analysis:

  1. Build Usage Map: Scan all files using regex patterns to identify where identifiers are used
  2. Analyze Definitions: Use regex patterns to find definitions and check against the usage map
  3. Apply Heuristics: Use naming conventions and context clues to improve accuracy

Both modes apply special heuristics for React components, Next.js entry points, and common patterns to minimize false positives.

Limitations & Future Improvements

  • AST-based Analysis: The default and recommended mode, providing high accuracy:
    • Complex Dynamic Usage: Code that is dynamically called or used through complex patterns might still be missed
    • Build-time Optimizations: Some dead code might be eliminated by build tools, making static analysis less relevant
    • Performance: AST parsing is slower than regex for very large codebases
  • Regex-based Analysis: Available as a faster alternative but with reduced accuracy:
    • False Positives: Code that appears unused to regex but is dynamically called or used in ways not captured by patterns
    • False Negatives: Code that is truly dead but is missed by the current patterns
    • Contextual Understanding: Struggles with advanced scenarios like HOCs, render props, or complex dependency injection
  • Unused Exports: Accurately identifying truly unused exports (i.e., code exported from a module but never imported by another module) requires building a full dependency graph of the entire project, which is significantly more complex than the current scope. The current tool focuses more on definitions that are not used anywhere.
  • Future Improvements:
    • ESLint Integration: Provide ESLint rules that leverage this analysis
    • IDE Integration: VSCode extension for real-time dead code detection
    • CI/CD Integration: GitHub Actions and other CI/CD platform integrations
    • Bundle Analysis: Integration with webpack bundle analyzer for more accurate size impact assessment
    • Enhanced AST Analysis: Further improvements to handle edge cases and dynamic patterns

Contributing

Contributions are welcome! If you find a bug, have a feature request, or want to contribute code, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License.