Package Exports
- @kitiumai/cyclic-dependency-fixer
- @kitiumai/cyclic-dependency-fixer/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 (@kitiumai/cyclic-dependency-fixer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
cyclic-dependency-fixer
๐ Detect and automatically fix cyclic dependencies in JavaScript/TypeScript projects with AI-powered analysis
cyclic-dependency-fixer
is a lightweight, powerful tool that not only detects circular dependencies but also provides intelligent auto-fix strategies powered by AI (Claude, GPT-4). Get context-aware recommendations, automated refactoring code, and actionable insights.
Table of Contents
- Features
- Installation
- Quick Start
- AI-Powered Features
- Usage
- Fix Strategies
- Output Example
- Architecture
- Testing
- Development
- Advanced Usage
- Contributing
- License
- Support
โจ Features
Core Features
- ๐ Fast Detection - Uses Tarjan's algorithm for efficient cycle detection (O(V + E))
- ๐ ๏ธ Auto-Fix Strategies - Attempts to automatically fix cycles when safe
- ๐ Manual Fix Guidance - Provides clear, actionable steps when auto-fix isn't possible
- ๐ฏ Multiple Strategies - Dynamic imports, shared module extraction, and more
- ๐ Clear Output - User-friendly CLI with colored output
- ๐ Extensible - Clean architecture allows custom fix strategies
- ๐ช Type-Safe - Written in TypeScript with strict typing
- ๐ชถ Lightweight - Minimal dependencies, uses regex-based parsing
๐ค AI-Powered Features (NEW!)
- Smart Strategy Selection - AI analyzes your code and recommends the best fix strategy
- Codebase Pattern Learning - Understands your architecture and coding patterns
- Intelligent Refactoring - Generates production-ready refactoring code
- Root Cause Analysis - Explains WHY circular dependencies exist
- Context-Aware Suggestions - Recommendations tailored to your codebase
- Multiple AI Providers - Support for Claude (Anthropic) and GPT-4 (OpenAI)
๐ Installation
npm install -g cyclic-dependency-fixer
Or use locally in your project:
npm install --save-dev cyclic-dependency-fixer
โก Quick Start
Basic Usage (No AI)
# Detect circular dependencies
cycfix detect
# Attempt to fix them automatically
cycfix fix --dry-run # Preview changes
cycfix fix # Apply fixes
With AI-Powered Analysis
# Set up your API key (one-time)
export ANTHROPIC_API_KEY=sk-ant-xxx # or OPENAI_API_KEY
# Run with AI-powered recommendations
cycfix fix --ai --generate-code
# Get detailed AI explanations
cycfix fix --ai --explain --generate-code
๐ค AI-Powered Features
Why Use AI?
Traditional static analysis can detect cycles but struggles with context. AI understands:
- Semantic relationships between modules
- Your codebase's architecture (layered, hexagonal, clean architecture, etc.)
- Common patterns you use (dependency injection, factory pattern, etc.)
- Why cycles exist (shared types, bidirectional relationships, etc.)
Setup
Get an API Key (choose one):
- Claude (Anthropic): https://console.anthropic.com/
- GPT-4 (OpenAI): https://platform.openai.com/api-keys
Set Environment Variable:
# For Claude (recommended) export ANTHROPIC_API_KEY=sk-ant-xxx # Or for GPT-4 export OPENAI_API_KEY=sk-xxx
Or use CLI flag:
cycfix fix --ai --ai-key sk-ant-xxx
AI Features
1. Smart Strategy Selection
AI analyzes your code and recommends the best fix strategy:
cycfix fix --ai
Output:
๐ค Analyzing codebase patterns with AI...
Architecture: Clean Architecture (Layered)
Patterns found: 3
๐ค Getting AI recommendation for cycle abc123...
Recommended: extract-shared (85% confidence)
Reasoning: Both UserService and OrderService depend on shared types.
Creating a shared types module maintains your layered architecture.
2. Intelligent Code Generation
Get production-ready refactoring code:
cycfix fix --ai --generate-code
Output:
๐ Manual steps to fix:
1. Create shared types module
File: src/shared/types/user-order.types.ts
export interface UserId {
readonly id: string;
}
export interface OrderReference {
readonly userId: UserId;
readonly orderId: string;
}
2. Update UserService imports
File: src/services/user.service.ts
Line: 3
- import { OrderReference } from './order.service'
+ import { OrderReference } from '../shared/types/user-order.types'
3. Root Cause Analysis
Understand WHY cycles exist:
cycfix fix --ai --explain
Output:
AI Analysis:
This circular dependency exists because:
1. UserService needs to track user orders (OrderReference type)
2. OrderService needs to validate users (UserId type)
3. Both services define types the other needs
Root Cause: Shared domain types without a common module
Impact:
- Prevents tree-shaking
- Can cause runtime initialization errors
- Makes testing harder (mocking circular deps)
Prevention:
- Create a shared types layer (src/types/)
- Follow the Dependency Inversion Principle
- Use interfaces to define contracts
4. Architecture-Aware Recommendations
AI adapts to YOUR codebase patterns:
โ Detected: You use dependency injection in 80% of services
โ Detected: Layered architecture with clear boundaries
โ Detected: Barrel files (index.ts) for public APIs
Recommendation: Use dependency injection pattern to break this cycle,
as it aligns with your existing architecture.
AI CLI Options
Option | Description |
---|---|
--ai |
Enable AI-powered analysis |
--ai-provider <provider> |
Choose provider: anthropic or openai (default: anthropic) |
--ai-key <key> |
Provide API key directly (or use env var) |
--explain |
Generate AI explanations of why cycles exist |
--generate-code |
Generate complete refactoring code with AI |
Example Workflow
# 1. Detect with AI analysis
cycfix fix --ai --dry-run
# 2. Review AI recommendations
# AI will show:
# - Detected architecture
# - Recommended strategy with confidence score
# - Reasoning for the recommendation
# 3. Generate refactoring code
cycfix fix --ai --generate-code --dry-run
# 4. Apply fixes
cycfix fix --ai --generate-code
๐ Usage
CLI
Detect Cycles
cycfix detect
Options:
-d, --dir <directory>
- Root directory to analyze (default: current directory)-e, --extensions <extensions>
- File extensions to include (default: .js,.jsx,.ts,.tsx)-x, --exclude <patterns>
- Patterns to exclude (comma-separated)--include-node-modules
- Include node_modules in analysis--max-depth <depth>
- Maximum depth for cycle detection (default: 50)
Example:
cycfix detect --dir ./src --extensions .ts,.tsx --exclude tests,__mocks__
Fix Cycles
cycfix fix
Options:
-d, --dir <directory>
- Root directory to analyze-e, --extensions <extensions>
- File extensions to include-x, --exclude <patterns>
- Patterns to exclude--dry-run
- Preview fixes without modifying files--no-backup
- Don't create backup files--auto
- Automatically apply fixes without confirmation
Example:
cycfix fix --dry-run # Preview changes
cycfix fix # Apply fixes with backups
Programmatic API
import { createAnalyzer } from 'cyclic-dependency-fixer';
const analyzer = createAnalyzer('./src');
// Detect cycles
const result = await analyzer.detect({
extensions: ['.ts', '.tsx'],
exclude: ['node_modules', 'dist'],
});
console.log(`Found ${result.cycles.length} cycles`);
// Attempt to fix
const { analysisResult, fixResults } = await analyzer.fix({
extensions: ['.ts', '.tsx'],
}, {
dryRun: false,
backup: true,
});
fixResults.forEach(result => {
if (result.success) {
console.log(`โ Fixed ${result.cycle.id}`);
} else {
console.log(`โ Manual steps required for ${result.cycle.id}`);
result.manualSteps?.forEach(step => {
console.log(` - ${step.description}`);
});
}
});
๐ฏ Fix Strategies
1. Dynamic Import Strategy
Converts static imports to dynamic imports to break the cycle.
Best for: Simple 2-node cycles where lazy loading is acceptable.
Example:
// Before
import { utils } from './utils';
// After
const { utils } = await import('./utils');
2. Extract Shared Strategy
Creates a new shared module to hold common code.
Best for: Modules in the same directory sharing common functionality.
Example:
Before: a.ts โ b.ts
After: a.ts โ shared.ts โ b.ts
๐ Output Example
๐ Analysis Results
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Total modules analyzed: 45
Analysis duration: 123ms
โ Found 2 circular dependencies
Affected modules: 4
Cycle #1 (abc12345)
src/components/Button.tsx โ
src/components/Form.tsx โ
src/components/Button.tsx โคด
Import details:
src/components/Button.tsx:5 imports src/components/Form.tsx
src/components/Form.tsx:12 imports src/components/Button.tsx
๐ง Fix Results
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Could not auto-fix cycle abc12345
Strategy attempted: dynamic-import
๐ Manual steps to fix:
1. Convert static import to dynamic import in src/components/Button.tsx
File: src/components/Button.tsx
Line: 5
// Replace:
// import x from './Form'
// With:
const x = await import('./Form').then(m => m.default || m);
2. Make the parent function async if needed
File: src/components/Button.tsx
๐๏ธ Architecture
Built with Clean Architecture principles:
src/
โโโ domain/ # Core business logic
โ โโโ models/ # Domain entities & types
โ โโโ interfaces/ # Abstractions (IFileSystem, IParser, etc.)
โโโ application/ # Use cases
โ โโโ DetectCyclesUseCase.ts
โ โโโ FixCyclesUseCase.ts
โ โโโ fix-strategies/ # Fix strategy implementations
โโโ infrastructure/ # External concerns
โ โโโ filesystem/ # File system operations
โ โโโ parsers/ # Code parsing
โ โโโ graph/ # Cycle detection algorithms
โโโ cli/ # Command-line interface
Key Design Principles
- โ SOLID Principles - Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion
- โ Dependency Injection - All dependencies injected for testability
- โ Strategy Pattern - Pluggable fix strategies
- โ No Any Types - Strict TypeScript typing throughout
๐งช Testing
Comprehensive test coverage (>90%):
npm test # Run all tests
npm run test:watch # Watch mode
๐ง Development
# Install dependencies
npm install
# Build
npm run build
# Lint
npm run lint
npm run lint:fix
# Format
npm run format
๐ Advanced Usage
Custom Fix Strategy
Extend the package with your own fix strategies:
import { IFixStrategy, FixStrategy, Cycle } from 'cyclic-dependency-fixer';
class MyCustomStrategy implements IFixStrategy {
readonly type = FixStrategy.CUSTOM as any;
async canFix(cycle: Cycle): Promise<boolean> {
// Your logic
return true;
}
score(cycle: Cycle): number {
// Higher score = preferred
return 100;
}
async fix(cycle, modules, fileSystem, dryRun) {
// Implement your fix
return { /* FixResult */ };
}
}
Integration with CI/CD
Add to your GitHub Actions:
- name: Check for circular dependencies
run: npx cycfix detect
๐ค Contributing
Contributions welcome! Please read our Contributing Guide first.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
๐ License
MIT ยฉ Ashish Yadav
๐ Acknowledgments
- Inspired by madge for cycle detection
- Uses Tarjan's algorithm for efficient SCC detection
- Built with TypeScript, Commander, Chalk, and Ora
๐ฎ Support
- ๐ Report a bug
- ๐ก Request a feature
- ๐ฌ Discussions
- โญ Star on GitHub
Made with โค๏ธ for the TypeScript community