question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot depend on dynamic created exports from another module.

See original GitHub issue

Bug 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, Aug 15, 2019

Angular modules are different (providers are available globally). In Nest, everything is isolated, encapsulated in the module scope.

1reaction
peterreiszcommented, Aug 14, 2019

@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 as FooModule. The only different is the forRoot 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

@Injectable()
export class FooService {}

@NgModule({})
export class FooModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: FooModule,
      providers: [FooService]
    };
  }
}

@Injectable()
export class BarService {
  constructor(readonly fooService: FooService) {}
}

@NgModule({
  imports: [FooModule],
  providers: [BarService]
})
export class BarModule {}

@NgModule({
  imports: [FooModule.forRoot(), BarModule],
})
export class AppModule {}

Can you provide me a solution for my use case?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found