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.
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
- Export the provider from its owning module if you query it at the root.
- Or call
moduleRef.get(Token, { strict: false })to search the whole application. - For scoped providers use
moduleRef.resolve()instead ofget().
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.
@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.