Skip to content
Latchkey

NestJS "Nest could not find X element" in CI

You asked the container for an instance with app.get() or moduleRef.get(), but the token is not visible in the module context you queried. Nest reports the element cannot be found.

What this error means

A bootstrap script or e2e setup fails with "Nest could not find X element (this provider does not exist in the current context)" when retrieving a service manually from the container.

NestJS
Error: Nest could not find MailerService element (this provider does not exist
in the current context)

Common causes

The token is not exported into the queried context

The provider exists in a submodule but is not exported, so app.get(MailerService) at the root cannot see it.

Retrieving a request-scoped provider without strict:false

Getting a scoped provider from the root context needs { strict: false }, otherwise Nest looks only in the current module and fails.

How to fix it

Resolve from the correct context

  1. Export the provider from its owning module if you query it at the root.
  2. Or call moduleRef.get(Token, { strict: false }) to search the whole application.
  3. For scoped providers use moduleRef.resolve() instead of get().
main.ts
const mailer = app.get(MailerService, { strict: false });

Export the provider so it is visible

Add the provider to the owning module's exports so importing contexts can retrieve it.

mailer.module.ts
@Module({ providers: [MailerService], exports: [MailerService] })
export class MailerModule {}

How to prevent it

  • Export any provider you retrieve manually from the container.
  • Use resolve() for request or transient scoped providers.
  • Prefer constructor injection over manual get() where possible.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →