title: How to import a registerAsync in a dynamic Nestjs module?
body: <p>been looking into the dynamic modules using the Advanced NestJS: <a href="https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370" rel="noreferrer"><strong>How to build completely dynamic NestJS modules.</strong></a></p>
  <p>From what I've seen, most people use this guide to build a sync/async dynamic module.</p>
  <p>But my question is, that if I use the registerAsync method, and my dynamic module needs to import HttpModule, and HttpModule's register-options are provided by my dynamic module.</p>
  <p>How do you import a module within a dynamic module, where the options are provided by the dynamic module?
  Or is it the wrong way to handle this issue? if so, how would you structure it?</p>
  <hr />
  <p>Here's the code. Which is practically a carbon copy of the tutorial.
  As you can see in the register method, it's simple - I just pass in the options.
  registerAsync however, I'm having trouble figuring out what to do.</p>
  <hr />
  <p>Any help is much appreciated :)</p>
  <pre><code>import { Module, DynamicModule, Provider, HttpModule } from &quot;@nestjs/common&quot;;
  import { InvoicesHealth } from &quot;./invoices/invoices.health&quot;;
  import { InvoicesResolver, InvoicesService } from &quot;./invoices&quot;;
  import {
    CustomerInvoicesOptions,
    CustomerInvoicesAsyncOptions,
    CustomerInvoicesOptionsFactory,
  } from &quot;./interfaces&quot;;
  import { CUSTOMER_INVOICES_OPTIONS } from &quot;./constants&quot;;
  import { createCustomerInvoicesProviders } from &quot;./providers/customer-invoices.providers&quot;;
  
  @Module({
    imports: [],
    controllers: [],
    providers: [InvoicesHealth, InvoicesResolver, InvoicesService],
    exports: [InvoicesHealth],
  })
  export class CustomerInvoicesModule {
    /**
     * Registers a configured customer-invoices Module for import into the current module
     */
    public static register(options: CustomerInvoicesOptions): DynamicModule {
      return {
        imports: [
          HttpModule.register({
            url: options.url,
            auth: {
              username: options.username,
              password: options.password,
            },
          }),
        ],
        module: CustomerInvoicesModule,
        providers: createCustomerInvoicesProviders(options),
      };
    }
  
    /**
     * Registers a configured customer-invoices Module for import into the current module
     * using dynamic options (factory, etc)
     */
    public static registerAsync(
      options: CustomerInvoicesAsyncOptions,
    ): DynamicModule {
      return {
        module: CustomerInvoicesModule,
        imports: options.imports || [],
        providers: [...this.createProviders(options)],
      };
    }
  
    private static createProviders(
      options: CustomerInvoicesAsyncOptions,
    ): Provider[] {
      if (options.useExisting || options.useFactory) {
        return [this.createOptionsProvider(options)];
      }
  
      return [
        this.createOptionsProvider(options),
        {
          provide: options.useClass,
          useClass: options.useClass,
        },
      ];
    }
  
    private static createOptionsProvider(
      options: CustomerInvoicesAsyncOptions,
    ): Provider {
      if (options.useFactory) {
        return {
          provide: CUSTOMER_INVOICES_OPTIONS,
          useFactory: options.useFactory,
          inject: options.inject || [],
        };
      }
  
      // For useExisting...
      return {
        provide: CUSTOMER_INVOICES_OPTIONS,
        useFactory: async (optionsFactory: CustomerInvoicesOptionsFactory) =&gt;
          await optionsFactory.createFtNestCustomerInvoicesOptions(),
        inject: [options.useExisting || options.useClass],
      };
    }
  }
  </code></pre>