Nest.js Library Module Dependency Resolution Issue
Published: 2023-08-29 10:45:55

I've encountered a dependency resolution issue while using a Nest.js library module called **`HttpHandlerModule`** in a different project called `Test-Nest-Npm-Library`. I've created the **`HttpHandlerModule`** library to handle HTTP requests using the Nest Axios library. The module is supposed to be configurable using options such as timeout. However, when I try to use this module in another project and configure it using a configuration file, I'm encountering the following error:
```
[Nest] ERROR [ExceptionHandler] Nest can't resolve dependencies of the HTTP_MODULE_OPTIONS (?). Please make sure that the argument [object Object] at index [0] is available in the HttpModule context.
```

I've gone through the documentation and relevant resources, but I haven't been able to identify the root cause of this issue. Here is some code that might help provide context:

**Library Module Code (HttpHandlerModule):**
```
@Global()
@Module({
  providers: [HttpHandlerService],
  exports: [HttpHandlerService]
})
export class HttpHandlerModule {
  static registerAsync(options: HttpModuleAsyncOptions): DynamicModule {
    return {
      module: HttpHandlerModule,
      imports: [
        HttpModule.registerAsync({
          useFactory: async (httpModuleOptions) => {
            return {
              timeout: httpModuleOptions.get('http.timeout')
            }
          },
          inject: [this.createAsyncProviders(options)]
        }),
      ],
      providers: this.createAsyncProviders(options)
    };
  }

  private static createAsyncProviders(
    options: HttpModuleAsyncOptions
  ): Provider[] {
    if (options.useExisting || options.useFactory) {
      return [this.createAsyncOptionsProvider(options)];
    }
    return [
      this.createAsyncOptionsProvider(options),
      {
        provide: options.useClass,
        useClass: options.useClass
      }
    ];
  }
  private static createHttpProvider(options: HttpOptions): any[] {
    return [{ provide: HTTP_MODULE_OPTIONS, useValue: options || {}, inject: [HTTP_MODULE_OPTIONS] }];
  }

  private static createAsyncOptionsProvider(
    options: HttpModuleAsyncOptions
  ): Provider {
    if (options.useFactory) {
      return {
        provide: HTTP_MODULE_OPTIONS,
        useFactory: options.useFactory,
        inject: options.inject || [],
      };
    } else if (options.useExisting) {
      return {
        provide: HTTP_MODULE_OPTIONS,
        useExisting: options.useExisting,
      };
    }
    return undefined;
  }

}
```
**Code to Use the Library Module in Another Project:**
```
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [configuration],
      cache: true
    }),
    HttpHandlerModule.registerAsync({
      useFactory: async (configService: ConfigService) => ({
        timeout: configService.get('http.timeout'),
      }),
      inject: [ConfigService],
    }),
  
  ],
  controllers: [],
  providers: [,
  ],
})
export class AppModule {}
```
The HttpHandlerModule module is meant to be flexible and configurable, but I seem to be missing something crucial here. Any insights into this issue or suggestions on how to troubleshoot it would be greatly appreciated.

Thank you in advance for your help!

**Update:**

Upon further investigation, I noticed that the issue might be related to how I'm trying to inject the configuration value into the HttpHandlerModule. I've posted the relevant code above. Is there a specific step I'm missing or a best practice that I should follow for injecting configuration values into a Nest.js library module?

@johnbiundo will you help me in resolving this issue