Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injection of core-providers not possible #7568

Closed
maxgr0 opened this issue Jul 14, 2021 · 8 comments
Closed

Injection of core-providers not possible #7568

maxgr0 opened this issue Jul 14, 2021 · 8 comments
Labels
needs triage This issue has not been looked into

Comments

@maxgr0
Copy link

maxgr0 commented Jul 14, 2021

Bug Report

Current behavior

After upgrading to 8.x.x, the injection of core providers like ModuleRef or ModulesContainer into normal Injectables is not possible anymore. Nothing changed, except upgrading @nestjs/core and others to 8.

Error: Nest can't resolve dependencies of the (...). Please make sure that the argument ModuleRef at index [0] is available in the KafkaModule context.

Potential solutions:
- If ModuleRef is a provider, is it part of the current KafkaModule?
- If ModuleRef is exported from a separate @Module, is that module imported within KafkaModule?
  @Module({
    imports: [ /* the Module containing ModuleRef */ ]
  })

Seems to happen here to: skunight/nestjs-redis#82

Input Code

constructor(
    private readonly moduleRef: ModuleRef,
    private readonly modulesContainer: ModulesContainer,
    private readonly metadataScanner: MetadataScanner,
) {}

Environment

Nest version: 8.0.0/8.0.1/8.0.2

@maxgr0 maxgr0 added the needs triage This issue has not been looked into label Jul 14, 2021
@jmcdo29
Copy link
Member

jmcdo29 commented Jul 14, 2021

Please provide a minimum reproduction?

@sgomez
Copy link

sgomez commented Jul 15, 2021

I have the same problem in nest 8.x (tested with node 14 and node 16). Steps to reproduce:

git clone https://github.com/aulasoftwarelibre/nestjs-eventstore
cd nestjs-eventstore
npm install && npm run build
cd example
npm install
npm run start:dev

The example directory use the builded module via file:.. in package.json

If you want to check that with nest 7.x works well you can checkout to an old release:

git clone https://github.com/aulasoftwarelibre/nestjs-eventstore
cd nestjs-eventstore
git checkout 0.1.5
npm install && npm run build
cd example
npm install
npm run start:dev

@jmcdo29
Copy link
Member

jmcdo29 commented Jul 15, 2021

This is working fine for me on a minimum reproduction, so I can only assume it's something in your code base. File links for package.json works rather strangely with Nest due to how these packages and their peer dependencies end up getting resolved, so a word of caution there.

But, with a very basic nest new <project> and adding constructor(private readonly moduleRef: ModuleRef) {} to the AppService and starting up the server, I have no errors in my logs.

@sgomez
Copy link

sgomez commented Jul 15, 2021

I cloned cqrs module, and build it. I have the same error when I load it with path instead version in package.json:

nest new demo
cd demo
npm install --save .../path/to/local/cqrs
# add CqrsModule to AppModule
npm run start:dev

and we get the same problem (and this didn't happens in 7.x). So this seems only a problem with local load. The point is that local load is useful when you are development a nestjs module.

@sgomez
Copy link

sgomez commented Jul 15, 2021

I did a workaround linking the local module src inside the example directory (instead using a local load in package) and it works. So this is not anymore an issue for me. Thanks for your time and support @jmcdo29.

@kamilmysliwiec
Copy link
Member

Nest v8 no longer stringifies class names but rather uses class references to connect injectables. That means all Nest packages should be defined as peer dependencies in case you want to share them (instead of having each package with its own copy of Nest deps).

@maxgr0
Copy link
Author

maxgr0 commented Jul 15, 2021

Nest v8 no longer stringifies class names but rather uses class references to connect injectables. That means all Nest packages should be defined as peer dependencies in case you want to share them (instead of having each package with its own copy of Nest deps).

Thanks for the fast reply @kamilmysliwiec. When having a package.json like that:

"peerDependencies": {
    "@nestjs/core": "^8.0.2",
    "@nestjs/common": "^8.0.2"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.2",
    "@nestjs/core": "^8.0.2",
    "@nestjs/common": "^8.0.2",
    "@nestjs/schematics": "^8.0.1"
  }
}

It still does not resolve the dependencies with the same error.
The full code can be found at the main branch here: https://github.com/jucr-io/nestjs-kafka-events. To be honest, I don't really get it why it's still not working and throwing errors at application start.

@kamilmysliwiec
Copy link
Member

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

Just FYI: reflect-metadata should be defined as peer dependency as well

@nestjs nestjs locked and limited conversation to collaborators Jul 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

4 participants