#Global Dynamic Modules not working

19 messages · Page 1 of 1 (latest)

radiant dune
#

Hello!

I have a weird issue. I have a created a reusable module that I published in a private registry. The module itself doesn't have the @Global() decorator as I don't want it to be global all the time, but I did use the extra options (https://docs.nestjs.com/fundamentals/dynamic-modules#extra-options) of the configurable module builder to extend the global field for the DynamicModule class. This is similar to nestjs/config where you can pass a isGlobal parameter to the module initialization.

Now, I have used it in another NestJS application and imported the module in app.module.ts with the global flag. However, other modules can't seem to resolve providers as dependencies coming from that module.

To check what I did wrong, I tried to create a wrapper module and added the @Global() decorator to it. I then imported the reusable module I made within it, and re-exported the providers I needed. I then added the wrapper module to app.module.ts instead -- and that worked.

What could possibly be wrong with my dynamic module setup? Do I really need to add the @Global() decorator to make it global?

EDIT: I forgot to mention that I'm trying to use it for an inject property for another dynamic module.

slim solstice
#

Can you show the code of your dynamic module and how you are consuming it in your app?

radiant dune
#

A sample below.

Dynamic Module

@Module({})
export class UploadModule extends ConfigurableModuleClass {
  static registerAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule {
    const { module, providers } = super.registerAsync(options);

    return {
      module,
      providers: [UploadService, ...providers],
      exports: [UploadService, ...providers],
    }
  }
}

This is the configurable module definition:

import { ConfigurableModuleBuilder } from '@nestjs/common';

export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, ASYNC_OPTIONS_TYPE } =
  new ConfigurableModuleBuilder({
    moduleName: 'upload-module',
  })
  .setExtras(
    {
      isGlobal: true,
    },
    (definitions, extras) => ({
      ...definitions,
      global: extras.isGlobal,
    }),
  )
  .build();

This is the app.module.ts on the NestJS application

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    UploadModule.registerAsync({
      useFactory: (configService: ConfigService) => {},
      inject: [ConfigService],
      isGlobal: true // this is where the extra options is
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

When I tried to import UploadService in some other module like below, startup has an error that can't resolve the dependency

@Module({
  imports: [
    SomeConfigurableModule.registerAsync({
      useFactory: (uploadService: UploadService) => {}
      inject: [UploadService] // this is where it throws an error on resolution
    })
  ]
})
#

Sorry @slim solstice, totally forgot to mention that I'm using it as dependency for another dynamic module initialization.

slim solstice
#

Is it correct that your useFactory functions are returning empty objects? 🤔

And besides that, I think you need to export the UploadService from the UploadModule. That's actually what makes it injectable anywhere else.

#

Hmm.. sorry. X that. I see you did. Hmmm... 🤔

radiant dune
#

useFactory returning empty objects,

Those are just examples. I have the correct objects being returned.

slim solstice
#

Try importing the UploadModule into the consuming module directly, and see if it is actually the global being the issue.

radiant dune
#

need to export UploadService from UploadModule

I did. See the exports field of the Dynamic Module.

radiant dune
slim solstice
#

Ok. And if you set the isGlobal in that consuming module (not app.module), what happens, when you use UploadService in other modules?

#

I'm not an expert on the intricacies of Nest's DI system, so I'm sort of poking in the dark. 🙂

radiant dune
slim solstice
#

Ok. I'm not certain where the issue lies then.

#

Sorry...

radiant dune
#

Totally, just wanted to see if someone here encountered the same issue or has an idea if I'm doing something wrong.

#

Thanks for responding still, @slim solstice!

slim solstice
radiant dune
#

Thanks for the tip! I actually looked at nestjs/config package, but it's basically the same concept. It just didn't use the Configurable Module Builder.