10

NestJS Cannot resolve dependencies of the UsersModule. Error:

Error: Nest can't resolve dependencies of the UsersModule (?). Please verify whether [0] argument is available in the current context.

app.module.ts:

@Module({
  imports: [
    ConfigModule,
    DatabaseModule,
    GraphQLModule,
    UsersModule,
  ],
  providers: [
    ErrorService,
  ],
  exports: [
    DatabaseModule,
    ErrorService,
  ],
})
export class AppModule implements NestModule {}

users.module.ts:

@Module({
    imports: [
        DatabaseModule,
        ErrorService,
    ],
    providers: [
        UsersService,
        ...usersProviders,
        UserResolver,
    ],
})
export class UsersModule {
    constructor(private readonly errorService: ErrorService) {}
}

Problem is this ErrorService, but for instance Database module is used in similar way, and it works without any error. I'm little confused) Maybe somebody would help. Thank you.

3
  • Vlad, I'm getting this message with booting up Nestjs 5 and filed a Github issue for it. So have others with v5. We may be chasing our tails trying to solve this. I think I just saw another post recently, like hours ago, with this same message. Commented Jun 12, 2018 at 17:21
  • Could share a link to the Github issue, please? @Preston Commented Jun 12, 2018 at 17:28
  • Oops, sorry. github.com/nestjs/nest/issues/723 Commented Jun 12, 2018 at 20:00

3 Answers 3

22

ErrorService is not properly injected in UsersModule.

It should either be:

  • In the providers of UsersModule
  • In the exports of one module imported by UsersModule

Otherwise, Nest won't be able to resolve it. And adding it to the exports of AppModule doesn't make it globally available, either.

I can see three solutions:

  • 1 - Adding ErrorService to the providers of UsersModule. But it doesn't look a proper way, as I think/guess that you will reuse this service in many other parts of the code (right?). Thus, better:
  • 2 - Create a ErrorsModule which exports the ErrorService, then UsersModule will import ErrorsModule and will be able to inject the service.

Like:

// errors.module.ts
@Module({
  providers: [ ErrorService ],
  exports: [ ErrorService ],
})
export class ErrorsModule {}

// users.module.ts
@Module({
  imports: [ErrorsModule],
})
export class UsersModule {
  constructor(private readonly errorService: ErrorService) {}
}
  • 3 - If your service is supposed to be reused (would LoggerService be a better name? If my guess is correct), you could use a @Global module that you'll import only once in your main module (AppModule)

Example:

@Global()
@Module({
  providers: [LoggerService],
  exports: [LoggerService],
})
export class LoggerModule {
  constructor(private readonly loggerService: LoggerService) { /* initialize logger, or whatever */ }
}

Then you must add LoggerModule to the imports of AppModule, and LoggerService will be injectable anywhere without having to re-declare it.

Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking about adding Errors Module, but I thought that is possible to inject service in this way. Your solution with the module is great! Thank you.
1

In my case it was a wrong used of the Inject function:

I had this:

export class UsersModule {
    constructor(
         @Inject()
         private readonly errorService: ErrorService
     ) {}

}

Instead of this:

export class UsersModule {
    constructor(private readonly errorService: ErrorService) {}
}

Comments

0

There is no need for you to import the error service because it is already imported in the users module your code should be like this: app.module.ts

  imports: [
    ConfigModule,
    DatabaseModule,
    GraphQLModule,
    UsersModule,
  ],
  providers: [
    ErrorService,
  ],
  exports: [
    DatabaseModule,
  ],
})
export class AppModule implements NestModule {}

user.module.ts

@Module({
    imports: [
        DatabaseModule,
        ErrorService,
    ],
    providers: [
        UsersService,
        ...usersProviders,
        UserResolver,
    ],
})
export class UsersModule {
    constructor(private readonly errorService: ErrorService) {}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.