lkaric / nestjs-twilio

Injectable Twilio client for Nestjs.
https://www.npmjs.com/package/nestjs-twilio
MIT License
42 stars 10 forks source link

Configurable module import dependency resolution error #43

Closed yoshitamagoshi closed 1 year ago

yoshitamagoshi commented 1 year ago

Bug Report

Describe the Bug

Thanks for the package! I'm getting the following error when attempting to import TwilioModule into another module using the service:

[Nest] 66091  - 2022-11-18, 11:13:07 p.m.   ERROR [ExceptionHandler] Nest can't resolve dependencies of the TwilioService (?). Please make sure that the argument CONFIGURABLE_MODULE_OPTIONS[fbec59cd-15f8-4bb2-804d-1987cd0a8ef6] at index [0] is available in the TwilioModule context.

Potential solutions:
- If CONFIGURABLE_MODULE_OPTIONS[fbec59cd-15f8-4bb2-804d-1987cd0a8ef6] is a provider, is it part of the current TwilioModule?
- If CONFIGURABLE_MODULE_OPTIONS[fbec59cd-15f8-4bb2-804d-1987cd0a8ef6] is exported from a separate @Module, is that module imported within TwilioModule?
  @Module({
    imports: [ /* the Module containing CONFIGURABLE_MODULE_OPTIONS[fbec59cd-15f8-4bb2-804d-1987cd0a8ef6] */ ]
  })

Error: Nest can't resolve dependencies of the TwilioService (?). Please make sure that the argument CONFIGURABLE_MODULE_OPTIONS[fbec59cd-15f8-4bb2-804d-1987cd0a8ef6] at index [0] is available in the TwilioModule context.

How to Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Add TwilioModule.forRootAsync... as defined in app.module.ts
  2. Add TwilioService to the constructor of a service
  3. Import TwilioModule in the *.module.ts for that service

Expected Behavior

No dependency injection error

Error

See above.

Your Environment

Error reproducing steps

Please explain how did your error ocurr, you can also leave gists, repos or any kind of codebase.

Additional Information

Any other information about the problem here.

lkaric commented 1 year ago

@yoshitamagoshi

From https://docs.nestjs.com/modules

The module encapsulates providers by default. This means that it's impossible to inject providers that are neither directly part of the current module nor exported from the imported modules. Thus, you may consider the exported providers from a module as the module's public interface or API.

Initialize the TwilioModule within the module in which you are using TwilioService.

Hopefully, soon, I'll be publishing the isGlobal flag which will allow you to initialize the module globally only once, and use the service through all of the modules.

Hope this helps!

lkaric commented 1 year ago

With version 3.2.0 you can pass the isGlobal property when initializing the TwilioModule.

Example:

app.module.ts

import { TwilioModule } from 'nestjs-twilio';

@Module({
  imports: [
    TwilioModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (cfg: ConfigService) => ({
        accountSid: cfg.get('TWILIO_ACCOUNT_SID'),
        authToken: cfg.get('TWILIO_AUTH_TOKEN'),
      }),
      isGlobal: true,
      inject: [ConfigService],
    }),
    SomeModule,
  ],
})
export class AppModule {}

some.module.ts

@Module({
  providers: [SomeService],
})
export class SomeModule {}

some.service.ts

@Injectable()
export class SomeService {
  constructor(private readonly twilioService: TwilioService) {}

  async getHello(): Promise<any> {
    return this.twilioService.client.messages.create({
      body: 'SMS Body, sent to the phone!',
      from: 'FROM_PHONE_NUMBER',
      to: 'TO_PHONE_NUMBER',
    });
  }
}

Anyhow, I'd advise you to read the previous comment and the linked resource

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.