Cannot depend on dynamic created exports from another module.
See original GitHub issueBug Report
FooModule.forRoot
creates configurable FooService.BarModule
imports FooModule and tries to provide BarService which depends on FooService- Resulting BarModule cannot finds FooService and cannot create BarService
Current behavior
Error: Nest can't resolve dependencies of the BarService (?). Please make sure that the argument at index [0] is available in the BarModule context.
at Injector.lookupComponentInExports (node_modules/@nestjs/core/injector/injector.js:183:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
at Function.Module.runMain (module.js:695:11)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
Input Code
import { Injectable, Module, DynamicModule } from "@nestjs/common";
@Injectable()
export class FooService {}
@Module({})
export class FooModule {
static forRoot(): DynamicModule {
return {
module: FooModule,
providers: [FooService],
exports: [FooService]
}
}
}
@Injectable()
export class BarService {
constructor(fooService: FooService) {}
}
@Module({
imports: [FooModule],
providers: [BarService],
exports: [BarService]
})
export class BarModule {}
@Module({
imports: [
FooModule.forRoot(),
BarModule,
]
})
export class AppModule {}
Expected behavior
BarModule should see all of FooModule-s exports, even dynamic ones
Possible Solution
When ModuleBar importing ModuleFoo, nest should wait for creating all of ModuleFoo exports (even dynamic ones) and then should try to initialize modules depending on ModuleFoo, for example ModuleBar.
Environment
Nest version: 6.5.2
For Tooling issues:
- Node version: 8 LTS
- Platform: Linux
Others:
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Dynamic exports in an AMD module - javascript - Stack Overflow
// A module that exports one function 'f'. The implementation of this f comes // from another module, dynamically selected based on a...
Read more >How to Dynamically Import ECMAScript Modules
The importing module uses import syntax to import a dependency: ... While the imported module exports its components using export syntax:.
Read more >Dynamic Module Imports in JavaScript - YouTube
With ES Modules came the ability to use the import statement and export statement to manage our JavaScript as modules.
Read more >Dynamic modules | NestJS - A progressive Node.js framework
A dynamic module can itself import other modules. We won't do so in this example, but if the dynamic module depends on providers...
Read more >JavaScript dynamic import() & export | by Andrea Giammarchi
For the same reason we have both import and export mechanisms to define our modules, we might want to also create modules that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Angular modules are different (providers are available globally). In Nest, everything is isolated, encapsulated in the module scope.
@kamilmysliwiec
My use case is to create dynamic service which I would like to configure only at root level. I though
FooModule.forRoot
is the same asFooModule
. The only different is theforRoot
provides services at root level (or where you import it).In angular it works as I described, you can try it here: https://codesandbox.io/s/angular-qld59
Can you provide me a solution for my use case?