question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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  

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
underfiskcommented, Mar 19, 2019

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

4reactions
frenchqwertycommented, Mar 19, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nestjs - Nest can't resolve dependencies of the AuthService ...
Nest can't resolve dependencies of the AuthService (?). Please make sure that the argument at index [0] is available in the AuthModule context....
Read more >
Nest can't resolve dependencies of the UsersService ... - Reddit
Please make sure that the argument PrismaService at index [0] is available in the UsersService context. I've read about 30 different S.O./reddit ...
Read more >
Common errors - FAQ - A progressive Node.js framework
Nest can't resolve dependencies of the <provider> (?). Please make sure that the argument <unknown_token> at index [<index>] is available in the <module> ......
Read more >
please make sure that the argument databaseconnection at ...
bash Error: Nest can't resolve dependencies of the AuthService (?). Please make sure that the argument at index [0] is available in the...
Read more >
Authentication in a Nest.js Application with Neo4j - Medium
Please make sure that the argument UserService at index [0] is available in the AuthModule context.Potential solutions: - If UserService is a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found