Circular Dependency Detected in Build (2026)

The Error

Warning: Circular dependency detected:
  src/models/user.ts -> src/services/auth.ts -> src/models/user.ts
This may cause undefined imports at runtime.

This error occurs when two or more modules import each other, directly or through a chain. Bundlers warn about it; at runtime, one of the imports resolves to undefined.

The Fix

  1. Detect all circular dependencies:
npx madge --circular --extensions ts src/
  1. Break the cycle by extracting shared types:
# Create a shared types file:
mkdir -p src/types
# Move shared interfaces to src/types/user.types.ts
// src/types/user.types.ts (new shared file)
export interface User { id: string; name: string; }
// src/models/user.ts (import from shared)
import { User } from '../types/user.types';
// src/services/auth.ts (import from shared, not from models)
import { User } from '../types/user.types';
  1. Verify the cycle is broken:
npx madge --circular --extensions ts src/
# Should output: No circular dependency found!

Why This Happens

Claude generates code that imports from the most logical module, but in large codebases this creates import chains that loop back to the originating module. At build time, one module in the cycle is not yet initialized when the other tries to import from it. The result is undefined values for what should be valid exports.

If That Doesn’t Work

  • Use lazy imports (dynamic import) to break runtime cycles:
// Instead of top-level import:
async function getAuth() {
  const { AuthService } = await import('./services/auth');
  return new AuthService();
}
  • Use dependency injection to decouple the modules.
  • Merge tightly coupled modules into a single file if they are small.

Prevention

Add this to your CLAUDE.md:

# Module Architecture
- No circular dependencies allowed. Run: npx madge --circular src/
- Shared types go in src/types/. Services import from types, not from each other.
- Use dependency injection for cross-service communication.

See Also

This fix also applies if you see variations of this error:

  • Connection or process errors with similar root causes in the same subsystem
  • Timeout variants where the operation starts but does not complete
  • Permission variants where access is denied to the same resource
  • Configuration variants where the same setting is missing or malformed

If your specific error message differs slightly from the one shown above, the fix is likely the same. The key indicator is the operation that failed (shown in the stack trace) rather than the exact wording of the message.

Frequently Asked Questions

Does this error affect all operating systems?

This error can occur on macOS, Linux, and Windows (WSL). The exact error message may differ slightly between platforms, but the root cause and fix are the same. macOS users may see additional Gatekeeper or notarization prompts. Linux users should check that the relevant system packages are installed. Windows users should ensure they are running inside WSL2, not native Windows.

Will this error come back after updating Claude Code?

Updates can occasionally reintroduce this error if the update changes default configurations or dependency requirements. After updating Claude Code, verify your project still builds and runs correctly. If the error returns, reapply the fix and check the changelog for breaking changes.

Can this error cause data loss?

No, this error occurs before or during an operation and does not corrupt existing files. Claude Code’s edit operations are atomic — they either complete fully or not at all. However, if the error occurs during a multi-step operation, you may have partial changes that need to be reviewed with git diff before continuing.

How do I report this error to Anthropic if the fix does not work?

Open an issue at github.com/anthropics/claude-code with: (1) the full error message including stack trace, (2) your Node.js version (node --version), (3) your Claude Code version (claude --version), (4) your operating system and version, and (5) the command or operation that triggered the error.

Try it: Paste your error into our Error Diagnostic for an instant fix.