typescript-circular-dependency

Detect and resolve circular import dependencies in TypeScript and JavaScript projects.

1|Updated May 6, 2026
One-click install
npx skills add https://github.com/surfingalien/FinSurfing --skill typescript-circular-dependency-surfingalien
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: typescript-circular-dependency
Source: https://github.com/surfingalien/FinSurfing/tree/main/.claude/skills/continuous-learning/examples/typescript-circular-dependency
Command: npx skills add https://github.com/surfingalien/FinSurfing --skill typescript-circular-dependency-surfingalien

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires madge.

What problem does it solve? Circular dependencies cause runtime errors like "Cannot access 'X' before initialization" and undefined imports even though TypeScript compiles successfully, making them hard to diagnose and fix. ## Core Features & Use Cases - Cycle Detection: Uses madge and TypeScript compiler output to find and visualize circular dependency chains in your codebase. - Five Resolution Strategies: Extract shared interfaces, apply dependency injection, use dynamic imports, switch to type-only imports, or restructure barrel files. - Prevention Setup: Configures ESLint import/no-cycle rules and CI checks to stop future cycles. - Use Case: Your Jest tests fail with undefined imports that work in the browser. Run madge to find the cycle between two services, then extract a shared interface to break it. ## Quick Start Ask the AI to find and fix the circular dependency causing "Cannot access before initialization" errors in your TypeScript project.

Frequently Asked Questions about typescript-circular-dependency

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I find circular dependencies in a TypeScript project?

Run madge with the --circular flag against your source directory, for example: madge --circular --extensions ts,tsx src/. It lists every cycle and can generate a visual dependency graph with the --image option.

How to fix "Cannot access before initialization" error in TypeScript?

This error usually indicates a circular import where a module is accessed before it finishes initializing. Break the cycle by extracting shared interfaces into a separate file, using dependency injection, or switching to type-only imports.

Why do Jest tests fail with undefined imports but the app works?

Jest and Vitest may resolve modules in a different order than your bundler, exposing circular dependencies that the bundler tolerates. Detect the cycle with madge and restructure the imports so no module depends on itself transitively.

Does import type prevent circular dependencies in TypeScript?

Yes, import type is erased at compile time and creates no runtime dependency, so it cannot participate in a cycle. Use it whenever you only need a type or interface rather than a runtime value.

How do I prevent circular imports in CI?

Add a madge --circular check as an npm script and run it in your CI pipeline, or configure the ESLint import plugin with the import/no-cycle rule. Both approaches fail the build when a new cycle is introduced.