#ClientsModule in library shipped as npm package

5 messages · Page 1 of 1 (latest)

pine thistle
#

Hi,

im using a microservices and i want to create a shared module. The module is build and deployed via an npm package and uses the ClientsModule.

If i integrate the code in one project everything is working.

my.module.ts

import { ClientsModule } from '@nestjs/microservices'

@Module({
    imports: [
        ClientsModule.registerAsync([
            {
                name: 'MY_CLIENT'
            }
        ])
    ],
    providers: [MyService],
    exports: [MyService]
})
export class MyModule {}

my.service.ts

import { Inject, Injectable } from '@nestjs/common'
@Injectable()
export class MyService {
    constructor(@Inject('MY_CLIENT') private readonly client: ClientProxy) {}
}

app.module.ts

import MyModule from './myModule.ts'
@Module({
    imports: [
        MyModule
    ]
})
export class AppModule {}

When i build the code of my.service.ts and my.module.ts in an npm package myLibary, export it via the index.ts and change the import to import MyModule from 'myLibary' i get this error

Error starting the application: UndefinedDependencyException [Error]: Nest can't resolve dependencies of the MY_CLIENT (?). Please make sure that the argument dependency at index [0] is available in the MyModule context.

Potential solutions:
- Is MyModule a valid NestJS module?
- If dependency is a provider, is it part of the current MyModule?
- If dependency is exported from a separate @Module, is that module imported within MyModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })

I checked the https://docs.nestjs.com/faq/common-errors page so its maybe a circular file import?

Any ideas or solutions? Thanks for the help.

pine thistle
#

The dependency of @nestjs/core and @nestjs/microservices are peerDependencies in myLibary and i checked there is only one source for the class ClientsModule

naive orbit
#

MY_CLIENT is a provider registered by ClientsModule.registerAsync. In this case, I don't see why it would cause a circular import

#

are you using NPM? at your app project, run npm ls @nestjs/microservices

pine thistle
#

Hi. Thanks for your help. I found my Problem. I was looking at the wrong position. The problem was importing the ConfigService in the useFactory method.

class MyOptionsFactory implements ClientsModuleOptionsFactory {
    constructor(@Inject(ConfigService) private readonly configService: ConfigService) {}

    createClientOptions() {
        return mqttRpcAndEventBusClientFactory(this.configService)
    }
}

import { ClientsModule } from '@nestjs/microservices'

@Module({
    imports: [
        ClientsModule.registerAsync([
            {
                name: 'MY_CLIENT',
                // useFactory not working
                useClass: MyOptionsFactory 
            }
        ])
    ],
    providers: [MyService],
    exports: [MyService]
})
export class MyModule {}