Orphix
Find unused files, exports, functions, components, APIs, and imports before they become technical debt.
Orphix uses static analysis and AST parsing via Babel to construct your project's dependency graph, identify orphaned files, discover unused module exports, locate dead local functions/React components, map dead API routes, identify unused npm package dependencies, and clean up the code automatically.
Features
- Unused File Detection: Identifies files that are completely unreachable from your entry points.
- Unused Export Detection: Flags module exports that are never imported anywhere in the project.
- Unused Function & Component Detection: Finds functions and React components declared but never invoked or rendered.
- Unused Imports Detection: Identifies imported bindings that are never referenced in the declaring file.
- Unused npm Dependencies: Flags third-party npm packages declared in
package.jsondependencies list that are never imported anywhere in the project. - Monorepo & Client/Server Support: Auto-groups workspaces containing separate sub-projects (e.g.
clientandserverdirectories) and runs entrypoint auto-detection per-project root to prevent cross-deletions. - Path Alias Resolution (
@/): Automatically resolves@/imports relative to the nearest project root'ssrcdirectory. - CommonJS & ES Modules: Full compatibility parsing for both standard ESM (
import/export) and CommonJS (require/module.exports). - Template-Literal Dynamic Imports: Traces and resolves template-literal dynamic imports (e.g.
import(\./pages/${page}`)`), automatically marking the target folders as reachable. - Framework Awareness: Out-of-the-box entrypoint auto-detection for Next.js (pages, layouts, API routes) and Vite.
- Barrel File / Re-export Propagation: Follows re-exports (e.g.
export { x } from './module') to trace usage back to its origin file correctly. - Dead API Route Tracing: Extracts Next.js API endpoints and scans string/template literals across the project to flag unused endpoints.
- Automatic Code Cleanup (
--clean): Rewrites files using Babel-traverse codemods to delete orphaned files, strip unused imports, and demote/remove unused exports. - Git History Integration: Attaches the last modification date and author to unused items to help coordinate cleanups.
- CI/CD Integration: Supports pipeline failures via the
--fail-on-dead-codeexit flag. - Babel AST Support: Out-of-the-box support for modern JavaScript, JSX, TypeScript, and TSX.
- Authoritative & Safe: Safe path validations prevent deletion of files outside the scan workspace boundary, and Code formatting preservation minimises Git diff lines.
Installation
Run Orphix directly without installation using npx:
npx orphixOr install it globally:
npm install -g orphixOr install it locally in your project:
npm install --save-dev orphixCLI Options
Running npx orphix --help outputs:
Usage: orphix [options] [dir]
Find unused files, exports, functions, and components in your JS/TS codebase
Arguments:
dir directory to scan (default: ".")
Options:
-V, --version output the version number
--json output result in JSON format (default: false)
--verbose detailed logs (default: false)
--fail-on-dead-code exit with code 1 if dead code is found (default: false)
--git extract git history metadata (last edit date/author) (default: false)
--ignore <patterns...> glob patterns of files to ignore
--entry <entryPoints...> explicit entry point files
--clean automatically delete unused files and clean up dead code (default: false)
-h, --help display help for command Configuration (orphix.config.json)
You can define a configuration file named orphix.config.json in the root of your target directory. It merges seamlessly with command-line options.
{
"entryPoints": ["src/index.js", "src/admin.js"],
"ignore": ["**/tests/**", "**/*.test.js"],
"git": true,
"failOnDeadCode": false,
"verbose": false,
"json": false
}Examples
Basic Scan
Scan the current directory (auto-detecting entry points):
npx orphixSpecify Target Directory
Scan a specific folder:
npx orphix src/Specify Entry Point
Instruct Orphix to trace dependencies starting from custom entry points:
npx orphix src/ --entry src/main.tsx src/admin.tsxEnable Git Integration
Retrieve information about when the files were last modified and by whom:
npx orphix --gitIgnore Directories
Specify custom patterns to ignore:
npx orphix --ignore "**/tests/**" "**/mock/**"Fail Build in CI/CD
Exits with exit code 1 if any unused files, exports, or functions are detected:
npx orphix --fail-on-dead-codeJSON Format
Print results in raw JSON format (ideal for custom pipelines and automated tools):
npx orphix --jsonAutomatic Code Cleanup
Automatically delete unused files, strip unused imports, and clean up exports/functions:
npx orphix --cleanHow It Works
- Scanner: Scans the targeted folder using
fast-glob. It respects default rules (ignoresnode_modules,.git,dist, etc.) and automatically parses.gitignorefile patterns if present. - Parser: Translates source code into Abstract Syntax Trees (AST) using
@babel/parsersupporting TypeScript and JSX. - Graph Builder: Traces static imports, dynamic imports, and CommonJS
require()calls to determine reachable code paths. - Analyzer: Cross-references imported specifiers and internal references to detect dead exports, unused imports, dead API endpoints, and propagates barrel re-exports.
- Cleaner: Modifies the file ASTs using
@babel/traverseand@babel/generatorto prune unused code, stripexportkeywords, remove empty import statements, and delete orphaned files. - Reporter: Displays detailed colorized summaries on stdout.
License
MIT