toeverything/AFFiNE · error · Error

Component must be created in the context of a provider

Error message

Component must be created in the context of a provider

What it means

Component's constructor reads the ambient CONSTRUCTOR_CONTEXT and throws when no provider is active, i.e. the component was instantiated with 'new' outside framework-managed creation (like a provider factory), which would leave it without DI services.

Source

Thrown at packages/common/infra/src/framework/core/components/component.ts:17

import { CONSTRUCTOR_CONTEXT } from '../constructor-context';
import type { FrameworkProvider } from '../provider';

export class Component<Props = {}> {
  readonly framework: FrameworkProvider;

  readonly props: Props;

  protected readonly disposables: (() => void)[] = [];

  get eventBus() {
    return this.framework.eventBus;
  }

  constructor() {
    if (!CONSTRUCTOR_CONTEXT.current.provider) {
      throw new Error('Component must be created in the context of a provider');
    }
    this.framework = CONSTRUCTOR_CONTEXT.current.provider;
    this.props = CONSTRUCTOR_CONTEXT.current.props;
    CONSTRUCTOR_CONTEXT.current = {};
  }

  dispose() {
    this.disposables.forEach(dispose => dispose());
  }

  [Symbol.dispose]() {
    this.dispose();
  }
}

View on GitHub (pinned to b4c8548c09)

Solutions

  1. Create the component inside a provider context, e.g. within framework.run or a provider scope.
  2. Do not instantiate components at module top level; defer creation until a provider is active.
  3. Check the stack trace for the component created outside any provider and move it.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/common/infra/src/framework/core/components/component.ts:17 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of toeverything/AFFiNE@b4c8548c09 (2026-08-18). Data as JSON: /api/errors/5004abd0149f679b. Report an issue: GitHub.