After_8
After_8

Reputation: 329

Inject service of parent module in NestJS

I have three Modules in NestJS: EndpointModule, JWTModule and the specific Endpoints as Modules (for Example InfoModule)

My EndpointModule looks like this:

@Module({
    imports: [
        JWTModule.withRSAKeys(
            Path.resolveByApp('./private.key'),
            Path.resolveByApp('./public.key')
        ),
        InfoModule,
        //More Endpoints
    ],
    exports: [JWTModule]
})
export class EndpointModule {}

JWTModule like this:

@Module({
    providers: [JWTService],
    exports: [JWTService]
})
export class JWTModule {
    static async withRSAKeys(
        privateKeyPath: string,
        publicKeyPath: string
    ): Promise<DynamicModule> {
        return {
            module: JWTModule,
            providers: await this.createProviders(privateKeyPath, publicKeyPath)
        };
    }

As you can see JWTModule is a dynamic module. Now i want to inject the exported JWTService in my endpoint controllers. For Example:

@Module({
    controllers: [InfoController]
})
export class InfoModule {}

@Controller()
export class InfoController {
    constructor(private jwt: JWTService){};

This does not work. I have to import the EndpointModule in my InfoModule but this creates a circular dependency. Are there any ways to avoid this? Should i reorder my Modules?

Upvotes: 4

Views: 5153

Answers (1)

Maciej Sikorski
Maciej Sikorski

Reputation: 773

You need to import JWTModule in InfoModule or decorate EndpointModule with Global()

Upvotes: 1

Related Questions