siddiq rehman
siddiq rehman

Reputation: 145

Nest JS cannot resolve dependencies in the service

I have created a core module which has a service within it.

import { Module } from '@nestjs/common';
import { CacheService } from './cache.service';


@Module({
  providers:[{ provide: CacheService, useValue: new CacheService()}],
  exports:[{ provide: CacheService, useValue: new CacheService()}]
})
export class CoreModule {}

I have imported the CoreModule in the AppModule.

@Module({
    imports: [
        TypeOrmModule.forRoot(typeOrmConfig),
        CoreModule,
        offerModule
    ],
    controllers: [AppController],
    providers: [AppService]
})
export class AppModule {}

There is a offer module imported in the AppModule. When I am trying to inject the CacheService in the checkmodule, it is throwing the error below.

2019-02-05 13:40:25 [error]:    Error: Nest can't resolve dependencies of the OfferService (OfferRepository, ?, Connection, AppLogger). Please make sure that the argument at index [1] is available in the OfferModule context.
    at Injector.lookupComponentInExports (C:\Project Workspace\ts-sharing\node_modules\@nestjs\core\injector\injector.js:144:19)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)
    at Function.Module.runMain (module.js:696:11)
    at Object.<anonymous> (C:\Project Workspace\ts-sharing\node_modules\ts-node\src\bin.ts:157:12)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
 1: v8::internal::compiler::Type::Negative31
 2: v8::internal::wasm::SignatureMap::Find
 3: v8::internal::Builtins::CallableFor
 4: v8::internal::Builtins::CallableFor
 5: v8::internal::Builtins::CallableFor
 6: 0000023CE08843C1

Can someone please help on this.

Upvotes: 1

Views: 1769

Answers (1)

Kim Kern
Kim Kern

Reputation: 60347

You have to import the CoreModule in your OfferModule as well, so that it is available for its providers.


If you want your CoreModule to be available globally, you can make it a global module with the @Global decorator. With it, the module has to be imported only once (e.g. in the AppModule) and will then be available in all modules.

@Global()
@Module({
  providers:[CacheService],
  exports:[CacheService],
})
export class CoreModule {}

Upvotes: 1

Related Questions