简体   繁体   中英

NestJS Error: “Nest can't resolve dependencies of the AuthService” even everything is wired up fine

In my nestjs application, I have a User and Auth module. Auth module depends on the User module for validating users.

Now, even though I wired up my dependencies properly in the module level, I am getting the dependencies resolution error (See the last part).

For reference, here are my module files and the relevant services.


src/user/user.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { UserService } from './services/user.service';
import { UserController } from './user.controller';
import { UserRepository } from './user.repository';

@Module({
  controllers: [UserController],
  imports: [TypeOrmModule.forFeature([UserRepository])],
  providers: [UserService],
  exports: [UserService],
})
export class UserModule {}

src/user/user.service.ts

/** imports... **/

@Injectable()
export class UserService {
  constructor(
    private readonly userRepository: UserRepository
  ) {
  }
  /** some codes **/
}

src/auth/auth.module.ts

import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';

import { UserModule } from '../user/user.module';
import { AuthController } from './auth.controller';
import { AuthService } from './services/auth.service';
import { JwtStrategy } from './strategies/jwt.strategy';

const jwtModule = JwtModule.register({
  secret: "SOME_SECRET",
  signOptions: { expiresIn: "1d" },
});

@Module({
  imports: [jwtModule, PassportModule, UserModule],
  providers: [AuthService, JwtStrategy],
  controllers: [AuthController],
  exports: [AuthService],
})
export class AuthModule {}

src/auth/auth.service.ts

import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

import { Status } from '../../shared';
import { UserEntity, UserService } from '../../user';

@Injectable()
export class AuthService {
  constructor(
    private readonly jwtService: JwtService,
    private readonly userService: UserService,
  ) {
  }
}

...and for reference, I have a barrel file in my user module folder.

src/user/index.ts

export * from './user.entity';
export * from './user.interface';
export * from './services/user.service';

app.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { AuthModule } from './auth/auth.module';
import { CoreModule } from './core/core.module';
import { ConfigService } from './core/services/config.service';
import { SharedModule } from './shared/shared.module';
import { UserModule } from './user/user.module';


/**
 * ORM configuration
 */
const typeORM = TypeOrmModule.forRoot({
  /** typeorm configuration **/
});

@Module({
  imports: [
    typeORM,
    CoreModule,
    SharedModule,
    UserModule,
    AuthModule,
  ],
})
export class AppModule {}

As you can see, I have my UserService class exported in the UserModule . UserService is also decorated with the @Injectable decorator. Finally, notice that I imported the UserModule in my AuthModule 's import section.

But when I try to run the app, I am presented with this error:

Nest can't resolve dependencies of the AuthService (JwtService, ?). Please make sure that the argument dependency at index [1] is available in the AuthModule context.

Given the above code files, there should be no reason for nest to not be able to locate the UserService within the AuthService . Am I missing something?

As @nerdy beast pointed out in the comment, the issue was actually related to importing file(s) from the barrel files. In my case, it is related to typeorm -- as discussed here https://github.com/typeorm/typeorm/issues/420#issuecomment-307219380 .

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