Inject nestjs service from another module
Javascriptnode.jsTypescriptDependency InjectionNestjsJavascript Problem Overview
I've got a PlayersModule
and an ItemsModule
.
I want to use the ItemsService
in the PlayersService
.
When I add it by injection:
import { Injectable } from '@nestjs/common';
import { InjectModel } from 'nestjs-typegoose';
import { ModelType, Ref } from 'typegoose';
import { Player } from './player.model';
import { Item } from '../items/item.model';
import { ItemsService } from '../items/items.service';
@Injectable()
export class PlayersService {
constructor(
@InjectModel(Player) private readonly playerModel: ModelType<Player>,
private readonly itemsService: ItemsService){}
I get this nest error :
> [Nest] 11592 - 2018-8-13 11:42:17 [ExceptionHandler] Nest can't > resolve dependencies of the PlayersService (+, ?). Please make sure > that the argument at index [1] is available in the current context.
Both modules are imported in the app.module.ts
. Both services are working alone in their module.
Javascript Solutions
Solution 1 - Javascript
You have to export the ItemsService
in the module that provides it:
@Module({
controllers: [ItemsController],
providers: [ItemsService],
exports: [ItemsService]
^^^^^^^^^^^^^^^^^^^^^^^
})
export class ItemsModule {}
and then import the exporting module in the module that uses the service:
@Module({
controllers: [PlayersController],
providers: [PlayersService],
imports: [ItemsModule]
^^^^^^^^^^^^^^^^^^^^^^
})
export class PlayersModule {}
⚠️ Do not add the same provider to multiple modules. Export the provider, import the module. ⚠️
Solution 2 - Javascript
Let' say you want to use AuthService from AuthModule in my TaskModule's controller
for that, you need to export authService from AuthModule
@Module({
imports: [
....
],
providers: [AuthService],
controllers: [AuthController],
exports:[AuthService]
})
export class AuthModule {}
then in TaskModule, you need to import AuthModule (note: import AuthModule not the AuthService in TaskModule)
@Module({
imports:[
AuthModule
],
controllers: [TasksController],
providers: [TasksService]
})
export class TasksModule {}
Now you should be able to use DI in TaskController
@Controller('tasks')
export class TasksController {
constructor(private authService: AuthService) {}
...
}
Solution 3 - Javascript
I solved my problem by removing @Inject()
from the argument in my constructor that was passing the exported service.
Solution 4 - Javascript
I believe that you faced the same problem i had. My scenario was 2 sibling custom modules (user, auth) that needed to use each other's services. I used circular DI to solve it. please check this link
Let me know whether if it solved your issue, maybe I can advise you further.
Solution 5 - Javascript
Solved my problem by changing the way of importing the constant string (TOKEN) used in @Inject()) of my provider... be careful using index.ts whith export * from module.ts, nest won't resolve the dependecy
Solution 6 - Javascript
The question is answered by Kim Kern. But I just want to remind people who read through this comment. Whenever you get this error, you should follow these steps that may help you easily figure out where the stuck is:
- Make sure the Module which provides providers was imported.
- Make sure the provider which you are using is exported.
For example, you have category module which contains category service, post module has post service and it has category service as a dependency:
@Module({
controllers: [CategoryController],
providers: [CategoryService],
exports: [CategoryService] // Remember to export
})
export class CategoryModule {}
And
@Module({
imports: [CategoryModule], // Make sure you imported the module you are using
controllers: [PostController],
providers: [PostService]
})
export class PostModule {}
Don't forget to use this annotation. Nest uses this to detect singleton class. In spring boot - Java, this one used to be called Bean. Read more:
@Injectable()
export class PostService {
constructor(private readonly categoryService: CategoryService // This will be auto injected by Nestjs Injector) {}
}
Solution 7 - Javascript
Based on the answer by Kim Kern nowadays we should add only injected service into our service without any decorators (@Inject() doesn't required). After that it will work right. That was my mistake and probably can help others.