#Share dynamic module in NestJS - what happens when the dynamic module is registered few times?

6 messages · Page 1 of 1 (latest)

dusky bane
#

I need to share a dynamic module. In the documentation I've found that we can share services in the global scope.
Yes, this is a solution. However, I would like to find out if it is possible on the module level. That is why I am interested in what happens when the dynamic module is called few times. From practice, I see that it creates different instances of modules in NestJS.
The module behaviour is different as it is in Angular however the NestJS likes to mention Angular similarity.
The property module takes me away. Now I understand that it is used only to control the module behavior. However, I do not see any
explanation in the documentation. I saw similar questions. But I've not found details that I am interested in.
Regards.

#

Does all modules global? That is why they behave like singletons, do not they?

eternal arch
#

NestJS likes to mention Angular similarity.
Similarity, not parity. They are similar not the same. Differences in behavior are expected

From practice, I see that it creates different instances of modules in NestJS.
This is correct. By convention we use names like forRoot when we expect to make something once and re-use it from the root module and register when we expect to have to register a module multiple times, but this is onyl community convention and not a hard rule at all. When you call DynamicModule.forRoot/register/Async() you will create a dynamic module in thecurrent module's context

If you want to share a dynamic module, I would suggest creating a wrapper around the dynamic module, so you have the wrapper call to the .forRoot()/register()/whaterver() and export the dynamic module by name (add exports: [DynamicModule]. This will allow you to import the wrapper and have one instance of the DynamicModule without having to make it a global instance

#

If you'd like, I'll add this answer to the StackOverflow question as well

dusky bane
#

Thank you. Your answer is good. I would appreciate your answer on StackOverflow.