Documents injecute 1.x · Migrating from 0.x
Skip to content

INJECUTE_CIRCULAR_DEPENDENCY ​

Services depend on each other in a cycle.

How to fix it ​

Break the cycle: extract the shared part into its own service, or resolve one side lazily with a function dependency (() => container.get(key)).

Example ​

ts
app
  .addSingleton('a', (b) => new A(b), ['b'])
  .addSingleton('b', (a) => new B(a), ['a']); // throws: a -> *b* -> a

Breaking the cycle ​

  • Move what both services need into a third service that neither depends on the other.
  • Resolve one side lazily with a function dependency, so it is only resolved when used:
ts
app.addSingleton('b', (getA) => new B(getA), [() => () => app.get('a')]);

Handling it in code ​

ts
import { InjecuteError } from 'injecute';

try {
  // ...
} catch (error) {
  if (error instanceof InjecuteError && error.code === 'INJECUTE_CIRCULAR_DEPENDENCY') {
    // error.message explains what happened; error.path is the resolution chain
  }
}

All error codes

Released under the MIT License.