7

I am using NestJS jwt passport to auth user. I follow the doc, here is my app module:

import { Module } from '@nestjs/common';
import { UserModule } from './user/user.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
import { ConfigModule } from '@nestjs/config';
import configuration from '../config/configuration';

@Module({
  imports: [
      ConfigModule.forRoot({
          load: [configuration],
      }),
      TypeOrmModule.forRoot(),
      UserModule,
      AuthModule,
  ],
})
export class AppModule {}

And here is my auth module:

import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { PassportModule } from '@nestjs/passport';
import { UserModule } from '../user/user.module';
import { LocalStrategy } from './local.strategy';
import { JwtStrategy } from './jwt.strategy';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { AuthController } from './auth.controller';

@Module({
    imports: [
        UserModule,
        PassportModule,
        JwtModule.registerAsync({
            imports: [ConfigModule],
            useFactory: async (configService: ConfigService) => ({
                secret:  configService.get('jwt.secret'),
                signOptions: {
                    expiresIn: configService.get('jwt.expiresIn'),
                },
            }),
            inject: [ConfigService],
        }),
    ],
    providers: [AuthService, LocalStrategy, JwtStrategy],
    exports: [AuthService],
    controllers: [AuthController],
})
export class AuthModule {}

Every time I run npm run start, I got this error message:

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.

I have searched this problem, but still can't fix this. Someone can help me? Thanks.

user11762401
  • 365
  • 1
  • 5
  • 9
  • 3
    Make `ConfigModule` global with `ConfigModule.forRoot({ isGlobal: true })` – Chau Tran Feb 17 '20 at 05:22
  • Hi, I answered this question at https://stackoverflow.com/questions/60182039/nest-js-cant-resolve-dependencies/60209470#60209470 @ChauTran answer is correct as well. – Tek Loon Feb 18 '20 at 00:59
  • 1
    To let everyone know, this [is a logged issue](https://github.com/nestjs/config/issues/82) on the ConfigModule repository. Waiting Kamil's reposnse – Jay McDoniel Feb 18 '20 at 06:11

2 Answers2

9

One option, as mentioned by others, is to make the ConfigModule global with the isGlobal: true setting. Otherwise, in @nestjs/config@0.2.3 it should be fixed and should now work as the docs show.

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
3

I fought with this same issue for hours but according to Chau Tran, after making ConfigModule global with ConfigModule.forRoot({ isGlobal: true }) all my problems were gone.

toonday
  • 501
  • 4
  • 13