简体   繁体   中英

NestJS can't resolve dependencies of the JWT_MODULE_OPTIONS (Same problem, different solution)

This is not duplicate! I saw same problem here , but solution not helped.

My NestJS instance is not starting because of this problem:

Nest can't resolve dependencies of the JWT_MODULE_OPTIONS (?). Please make sure that the argument ConfigService at index [0] is available in the JwtModule context.

My auth.module.ts:

@Module({
  imports: [
    TypeOrmModule.forFeature([User, Token]),
    DatabaseModule,
    UserModule,
    ConfigModule,
    PassportModule,
    JwtModule.registerAsync({
      imports: [ConfigModule], // Missing this
      useFactory: async (configService: ConfigService) => ({
        signOptions: {
          expiresIn: configService.get<string>('JWT_EXPIRATION'),
        },
        secretOrPrivateKey: configService.get<string>('JWT_SECRET'),
      }),
      inject: [ConfigService],
    }),
  ],
  controllers: [AuthController],
  providers: [AuthService, LocalStrategy],
})
export class AuthModule {}

and this is my app.module.ts (Entry Module):

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: '.development.env',
    }),
    AuthModule,
    UserModule,
    DatabaseModule,
  ],
})
export class AppModule {}

I am importing module correctly by imports: [ConfigModule] , but still I am getting error, that injection failed, because module is not imported.

My ConfigModule is 100% imported into app as my log say: ConfigModule dependencies initialized

What am I doing wrong?

The issue, assuming you are using the @nestjs/config module, was a bug in the module's code for version < 0.2.3. In the latest version there was a fix to the issue. If you upgrade to 0.2.3 with npm i @nestjs/config@latest it should run fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM