#provider undefined unless I use `@Inject`

16 messages · Page 1 of 1 (latest)

inland agate
#

Has anyone here experienced this issue with Nestjs failing to inject some providers automatically?

This works:

@Injectable()
export class CognitoTriggerService {
  constructor(@Inject(UserService) private readonly userService: UserService) {}

  // ...
}

But without @Inject, userService is undefined (nest doesn't throw any errors saying it failed to resolve)

@Injectable()
export class CognitoTriggerService {
  constructor(private readonly userService: UserService) {}

  // ...
}

This is strangely only happening with this one injection. I'm injecting UserService into other providers and controllers without using @Inject

inland thistle
#

That’s probably due to some circular import

#

You can use madge CLI tool to find that out

inland agate
#

I wouldn't think adding @Inject(UserService) would fix that

#

Gonna try madge. I don't think I've got any circular dependencies, but I'll see what it says

elder lava
#

Where is the service used? Is it possibly becoming request scoped in a context that it shouldn't be

inland thistle
inland agate
#

Sorry haven't had time to dig into this further. My specific project structure makes using madge difficult (NX + AWS CDK + lambda). The provider I'm having issues with is being resolved in the lambda handler using app.resolve. It looks like this:

let app: INestApplication;
export const handler: Handler<EventType, unknown> = async (event, context) => {
  if (!app) {
    app = await NestFactory.create(LambdaHandlerModule.register(CognitoTriggerModule));
    app.useGlobalPipes(createValidationPipe());
    await app.init();
  }
  const cognitoTriggerService = await app.resolve(CognitoTriggerService);
  return cognitoTriggerService.handleCognitoTriggerEvent(event);
};

The LambdaHandlerModule just imports a global module alongside CognitoTriggerModule

import { DynamicModule, Module } from "@nestjs/common";
import { GlobalModule } from "../modules/Global/Global.module";

export type LambdaHandlerAppModule = { new (): unknown } | DynamicModule;

/**
 * This is the root module used in nest lambda functions.
 * Provides the global module to the app module.
 */
@Module({})
export class LambdaHandlerModule {
  static register(appModule: LambdaHandlerAppModule): DynamicModule {
    return {
      module: this,
      imports: [GlobalModule, appModule],
    };
  }
}
#

The global module is where UserService is exposed (exported from UserModule)

import { Global, Module } from "@nestjs/common";
import { PrismaModule } from "../Prisma/Prisma.module";
import { UserModule } from "../User/User.module";

@Global()
@Module({
  imports: [PrismaModule, UserModule],
  exports: [PrismaModule, UserModule],
})
export class GlobalModule {}

Also here's the CognitoTriggerModule definition

import { Module } from "@nestjs/common";
import { CognitoTriggerService } from "./CognitoTrigger.service";

@Module({
  providers: [CognitoTriggerService],
  exports: [CognitoTriggerService],
})
export class CognitoTriggerModule {}

export * from "./CognitoTrigger.service";
elder lava
#

Is CognitoTriggerService request scoped? And is it Christmas with UserService?

inland agate
#

Nope not request scoped. Christmas?

elder lava
#

Circular* gah, mobile typos

elder lava
inland agate
#

There's no circular dependencies that I can find. UserService only has 2 dependencies (EmailService and PrismaService). Neither of which have any dependencies themselves.

elder lava
#

What's the reason you're using app.resolve instead of app.get?

inland agate
#

Not sure - I'll try app.get