francadaval
francadaval

Reputation: 2481

Why does a dynamic NestJS module built with ConfigurableModuleBuilder throw dependency error when imported from external project?

I created a module in a library that requires some configuration parameters. I'm using ConfigurableModuleBuilder following instructions on NestJS documentation so register and registerAsync functions are created:

const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } =
    new ConfigurableModuleBuilder<RepositoriesModuleOptions>().build();

@Module({
    providers: [
        {                    
            provide: ConnectionService,
            useFactory: (options: RepositoriesModuleOptions) => 
                new ConnectionService(options.connectionUri, options.mongoClientOptions),
            inject: [MODULE_OPTIONS_TOKEN],
        },
        RepositoryMethodsBuilder,
        ...MethodBuilderProviders(METHOD_BUILDERS),
        ...FilterModifierProviders(FILTER_MODIFIERS)
    ],
    exports: [
        ConnectionService,
        RepositoryMethodsBuilder
    ]
})
export class RepositoriesModule extends ConfigurableModuleClass {}

In the same project, I implemented some e2e tests with a module that imports this one and it works fine with both methods (register and registerAsync):

@Module({
    imports: [
        RepositoriesModule.registerAsync({
            useFactory: () => {
                Logger.log("Registering RepositoriesModule");
                return {
                    connectionUri: TEST_URI
                };
            }
        })
        //RepositoriesModule.register({
        //    connectionUri: TEST_URI
        //})
    ],
    providers: [
        repositoryFactoryProvider(TestRepo),
        repositoryFactoryProvider(TestRepo2),
        repositoryFactoryProvider(ExtendedEntityRepository)
    ]
})
export class TestModule {}

In another NestJS project, I am importing this library locally and trying to import the RepositoriesModule in the same way. The library worked previously to convert the module to a dynamic module.

@Module({
    imports: [
        ConfigModule.forRoot({ isGlobal: true }),
        UserModule,
        RepositoriesModule.register({
            connectionUri: TEST_URI
        })
    ],
    controllers: [AppController],
    providers: [AppService]
})
export class AppModule {}

In that case, I get this error when I run the application, for both register and registerAsync methods:

Error: Nest can't resolve dependencies of the ConnectionService (?). Please make sure that the argument "CONFIGURABLE_MODULE_OPTIONS[97243f5614591d9255421]" at index [0] is available in the RepositoriesModule context.

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

    at Injector.lookupComponentInParentModules (\full-stack-poc\node_modules\@nestjs\core\injector\injector.js:254:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Injector.resolveComponentInstance (\full-stack-poc\node_modules\@nestjs\core\injector\injector.js:207:33)
    at async resolveParam (\full-stack-poc\node_modules\@nestjs\core\injector\injector.js:128:38)
    at async Promise.all (index 0)
    at async Injector.resolveConstructorParams (\full-stack-poc\node_modules\@nestjs\core\injector\injector.js:143:27)
    at async Injector.loadInstance (\full-stack-poc\node_modules\@nestjs\core\injector\injector.js:70:13)
    at async Injector.loadProvider (\full-stack-poc\node_modules\@nestjs\core\injector\injector.js:97:9)
    at async Injector.lookupComponentInImports (\full-stack-poc\node_modules\@nestjs\core\injector\injector.js:289:17)
    at async Injector.lookupComponentInParentModules (\full-stack-poc\node_modules\@nestjs\core\injector\injector.js:252:33)

What and why is causing this issue in the external project but not in the inner e2e tests?

Upvotes: 1

Views: 66

Answers (0)

Related Questions