# TypeScript circular dependency resolver

> Finds and fixes circular import bugs in your code.

- **What it does:** Finds and fixes a specific coding bug where two files in your TypeScript or JavaScript project import from each other and cause confusing crashes, then explains how it got fixed.
- **Agents:** Claude Code and Codex (through orbit-connect), Claude and ChatGPT (through the Orbit connector)
- **Install:** `npx -y --loglevel=error orbit-connect@0.3.33 install skl_8101cd2f297a89ad63910a35`
- **Creator:** Mariah Brunner (@itsmariahbrunner) on Instagram
- **Safety:** Checked by Orbit: nothing risky found in what it runs or reads
- **Last updated:** 2026-11-14
- **Page:** https://orbitinbox.app/s/typescript-circular-dependency-resolver

## Skill

---
name: fix-circular-imports
description: Use when fixing TypeScript/JavaScript circular imports: "Cannot access 'X' before initialization", imports that come back undefined, or tests failing with empty imports.
---

# Fix circular imports

## What this does
Finds files that import each other in a loop, which causes runtime errors like "Cannot access 'X' before initialization" or imports that come back undefined. Then it changes the code to break the loop.

## When to use
- "ReferenceError: Cannot access 'X' before initialization" at runtime
- An import returns undefined unexpectedly
- Type errors that disappear when you change import order
- Jest/Vitest tests fail with undefined imports that work in the browser

## How to run it
1. Ask the user for the runtime error text, or a description of the import that comes back undefined: {error_or_symptom}.
2. Find the source folder: {src_dir} (default: src). Run `npx madge --circular {src_dir}` to list the import cycles.
3. For each cycle reported, open the files involved and identify what each one imports from the other.
4. Break the loop with the smallest change that fits. Options named by the creator:
   - Move shared types or contracts into a separate shared interfaces file that both modules import.
   - Change imports that are only used for types into type-only imports (`import type { ... }`).
5. Re-run `npx madge --circular {src_dir}` and confirm no cycles remain.
6. Re-run the failing code or tests and confirm the original error is gone.

## Output
- The list of cycles found and which one caused the error
- The code changes made to break each cycle
- Confirmation that madge reports no cycles and the original error no longer occurs

## Credit
Based on public content by blader, packaged by Orbit.

Shared by Mariah Brunner, credited on Orbit.