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

Created on 19 Mar 2019  路  9Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


I get :

Error: Nest can't resolve dependencies of the AuthService (?). Please make sure that the argument at index [0] is available in the AuthModule context.

Expected behavior


The app should run and the AuthService dependancies can be injected in AuthModule

Minimal reproduction of the problem with instructions


app.module.ts

@Module({
    imports: [
        TypeOrmModule.forRoot(),
        UserModule,
        AuthModule
    ],
    controllers: [AppController, UserController],
    providers: [AppService, UserService],
})
export class AppModule {
    constructor(private readonly connection: Connection) {

    }
}

auth.module.ts :

@Module({
    imports: [UserModule],
    providers: [AuthService, HttpStrategy]
})
export class AuthModule {
}

http.strategy.ts :

@Injectable()
export class HttpStrategy extends PassportStrategy(Strategy) {
    constructor(private readonly authService: AuthService) {
        super();
    }

    async validate(token: string) {
        const user = await this.authService.validateUser(token);
        if (!user) {
            throw new UnauthorizedException();
        }
        return user;
    }
}

auth.service.ts :

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

    async validateUser(token: string): Promise<any> {
        // Validate if token passed along with HTTP request
        // is associated with any registered account in the database
        return await this.userService.findOneByToken(token);
    }
}

What is the motivation / use case for changing the behavior?


I want to add an Authentication system in my app.

Environment


Nest version: 5.4.0

For Tooling issues:

  • Node version: 10.15.0
  • Platform: Windows
needs clarification

Most helpful comment

Hi, i think you are missing the export of the provider (you are trying to use the service in a different module)
I have ran into the same issue before

All 9 comments

Please, provide a minimal repository which reproduces your issue.

Hi, i think you are missing the export of the provider (you are trying to use the service in a different module)
I have ran into the same issue before

My issues is solved !
The solution is :
auth.module.ts :

@Module({
    imports: [
        UserModule,
        PassportModule.register({defaultStrategy: 'bearer'})
],
    providers: [AuthService, HttpStrategy, UserService]
})
export class AuthModule {
}

Just add the UserService in provider.

Glad to hear ;)

Instead of adding UserService twice (in UserModule and AuthModule), simply export it from UserModule (add UserService to exports array)

Instead of adding UserService twice (in UserModule and AuthModule), simply export it from UserModule (add UserService to exports array)

exuse me why does this answer ?

wondering if anyone has insight into why this isnt working

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

UserSerivice is being imported via the module into the AuthModule. I can use TopicModule fine

https://github.com/ricardosaracino/collab-apiapp/blob/master/src/users/users.service.ts

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

janckerchen picture janckerchen  路  3Comments

VRspace4 picture VRspace4  路  3Comments

cdiaz picture cdiaz  路  3Comments

KamGor picture KamGor  路  3Comments

tronginc picture tronginc  路  3Comments