这不是重复的!我看到了相同的问题here,但是解决方案没有帮助。

由于以下问题,我的NestJS实例无法启动:

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.


我的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 {}


这是我的app.module.ts(入口模块):

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


我正在通过imports: [ConfigModule]正确导入模块,但是仍然出现错误,注入失败,因为未导入模块。

我的ConfigModule已100%导入到应用程序中,正如我的日志所说:ConfigModule dependencies initialized

我究竟做错了什么?

最佳答案

假设您使用的是@nestjs/config模块,则该问题是模块代码中In the latest version该问题已修复。如果使用npm i @nestjs/config@latest升级到0.2.3,它将运行正常。

09-20 09:16