nestjs/nest · critical · UnknownExportException

Nest cannot export a provider/module that is not a part of t

Error message

Nest cannot export a provider/module that is not a part of the currently processed module (${module}). Please verify whether the exported ${token} is available in this particular context.

Possible Solutions:
- Is ${token} part of the relevant providers/imports within ${module}?

For more common dependency resolution issues, see: https://docs.nestjs.com/faq/common-errors

What it means

Modules may only export what they own or what they import: `validateExportedToken` checks every entry of the `exports` array against the module's providers and imported module classes. If the token is neither, NestJS throws UnknownExportException ('Nest cannot export a provider/module that is not part of the currently processed module') at bootstrap. Exporting is a re-export, not a forward declaration.

Source

Thrown at packages/core/injector/module.ts:499

      return this._exports.add(this.validateExportedProvider(provide));
    }
    this._exports.add(this.validateExportedProvider(provide));
  }

  public validateExportedProvider(token: InjectionToken) {
    if (this._providers.has(token)) {
      return token;
    }
    const imports = iterate(this._imports.values())
      .filter(item => !!item)
      .map(({ metatype }) => metatype)
      .filter(metatype => !!metatype)
      .toArray();

    if (!imports.includes(token as Type<unknown>)) {
      const { name } = this.metatype;
      const providerName = isFunction(token) ? (token as Function).name : token;
      throw new UnknownExportException(providerName as string, name);
    }
    return token;
  }

  public addController(controller: Type<Controller>) {
    this._controllers.set(
      controller,
      new InstanceWrapper({
        token: controller,
        name: controller.name,
        metatype: controller,
        instance: null!,
        isResolved: false,
        scope: getClassScope(controller),
        durable: isDurable(controller),
        host: this,
      }),
    );

View on GitHub (pinned to a3a31b9643)

Solutions

  1. If the exported provider is declared locally, make sure the exact same token is in this module's `providers`.
  2. If it comes from an imported module, export the imported module class instead: `exports: [FeatureModule]` with `imports: [FeatureModule]`.
  3. Re-declare the provider in this module (`providers: [SomeService]`) if it should genuinely be provided here.
  4. Check for token drift: `{ provide: 'X', ... }` must be exported as `'X'`, not as the class.

Example fix

// before
@Module({
  imports: [CatsModule],          // CatsModule provides CatsService
  exports: [CatsService],         // not owned by THIS module -> error
})
export class PetsModule {}

// after
@Module({
  imports: [CatsModule],
  exports: [CatsModule],          // re-export the module that owns it
})
export class PetsModule {}
Defensive patterns

Strategy: validation

Validate before calling

// Fail fast at test time: exports must be a subset of providers ∪ imports
import 'reflect-metadata';

function assertExportsDeclared(module: Function) {
  const declared = new Set<any>([
    ...(Reflect.getMetadata('providers', module) ?? []),
    ...(Reflect.getMetadata('imports', module) ?? []),
  ]);
  for (const token of Reflect.getMetadata('exports', module) ?? []) {
    if (!declared.has(token)) {
      throw new Error(
        `${module.name} exports ${String(token)} which is neither a provider nor an import`,
      );
    }
  }
}

Try / catch

try {
  await NestFactory.create(AppModule);
} catch (e: any) {
  if (/cannot export a provider/module/i.test(String(e?.message))) {
    console.error(e.message); // names the token and module — fix exports/providers there
    process.exit(1);
  }
  throw e;
}

Prevention

When it happens

Trigger: `exports: [SomeService]` where SomeService is provided by another module that is imported — you must export the imported module itself; exporting a token that was never declared in `providers`; exporting a provider from a DynamicModule while the static `exports` references a different token; copy-pasting an exports array from another module.

Common situations: Building feature modules that aggregate several sub-modules; refactoring providers between modules without updating exports; exporting interfaces/custom tokens whose provider is declared elsewhere; using forRoot()-style dynamic modules and exporting tokens the dynamic wrapper never declared.

Related errors


AI-assisted analysis of nestjs/nest@a3a31b9643 (2026-08-21). Data as JSON: /api/errors/896a7e85d7814be5. Report an issue: GitHub.