0
votes

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?

1
can you show your aap.module.ts file ? - Anton Skybun
Hi @AntonSkybun, updated the OP with the app.module.ts file as you asked for. - Aldee
strange, look norm. Can you change order of imports module in here imports: [jwtModule, PassportModule, UserModule], try to set USerModule first - Anton Skybun
I can see your UserModule is getting the UserService directly from import { UserService } from './services/user.service'; while your AuthService is getting it from the barrel file import { UserEntity, UserService } from '../../user';. This shouldn't matter but try changing these imports to be the same thing and see if that makes a difference. - nerdy beast
Hi @nerdybeast, You are partially correct! but the issue was actually importing an entity thru a barrel file. The typeorm library that I am using seems to have difficulty making relationships when importing via barrel files. I changed that part and it worked! However, I will have uniformity issues with my coding as I have to explicitly define the complete path of the file I am importing only for entities -- which I will find some alternatives. Nevertheless, thanks for your help. - Aldee

1 Answers

0
votes

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.