Papooch / nestjs-cls

A continuation-local storage (async context) module compatible with NestJS's dependency injection.
https://papooch.github.io/nestjs-cls/
MIT License
390 stars 23 forks source link

Cannot resolve dependencies of ClsModule #77

Closed linardsblk closed 11 months ago

linardsblk commented 12 months ago

Hi, after implementing ClsModule within my application, I get this dependency error and I am unsure on how to fix. Does anyone have any guidance?

[Nest] 66508  - 07/07/2023, 11:57:53 AM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the ClsModule (?, ModuleRef). Please make sure that the argument HttpAdapterHost at index [0] is available in the ClsModule context.

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

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

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

    at Injector.lookupComponentInParentModules (/Users/linards/Documents/projects/fripro/apps/server/node_modules/@nestjs/core/injector/injector.js:248:19)
    at Injector.resolveComponentInstance (/Users/linards/Documents/projects/fripro/apps/server/node_modules/@nestjs/core/injector/injector.js:202:33)
    at resolveParam (/Users/linards/Documents/projects/fripro/apps/server/node_modules/@nestjs/core/injector/injector.js:123:38)
    at async Promise.all (index 0)
    at Injector.resolveConstructorParams (/Users/linards/Documents/projects/fripro/apps/server/node_modules/@nestjs/core/injector/injector.js:138:27)
    at Injector.loadInstance (/Users/linards/Documents/projects/fripro/apps/server/node_modules/@nestjs/core/injector/injector.js:64:13)
    at Injector.loadProvider (/Users/linards/Documents/projects/fripro/apps/server/node_modules/@nestjs/core/injector/injector.js:91:9)
    at /Users/linards/Documents/projects/fripro/apps/server/node_modules/@nestjs/core/injector/instance-loader.js:56:13
    at async Promise.all (index 0)
    at InstanceLoader.createInstancesOfProviders (/Users/linards/Documents/projects/fripro/apps/server/node_modules/@nestjs/core/injector/instance-loader.js:55:9)

I implemented it just like in docs by adding module to AppModule imports

ClsModule.forRoot({
      global: true,
      middleware: { mount: true },
    }),
Papooch commented 12 months ago

Are you by chance consuming it via a custom npm package?

Papooch commented 12 months ago

@linardsblk Hello, could you please provide a little more clarifying information? It is impossible for me to guess what might be wrong from the error message only.

AvnerMaster commented 11 months ago

Hey ! I have this problem too, I'm using 3.5.0 version of nestjs-cls and 10.0.5 version of @nestjs/core

Papooch commented 11 months ago

@AvnerMaster are you using the nestjs-cls library directly in your app, or through a linked library?

AvnerMaster commented 11 months ago

Directly, it is a dependency in my package.json

Papooch commented 11 months ago

@AvnerMaster I just tried with a fresh installation of Nest 10.0.5 and nestjs-cls 3.5.0 and did not notice any issues. Could you provide a repository where this error is reproduced?

My other guess would be that your @nestjs/* dependencies are not properly matched.

For the record, it is working with these versions:

"@nestjs/common": "^10.0.5",
"@nestjs/core": "^10.0.5",
"@nestjs/platform-express": "^10.0.5",
"nestjs-cls": "^3.5.0",
Papooch commented 11 months ago

The error seems very similar to this one and this one. In both cases, the error is most likely caused by mismatched versions of @nestjs/common and @nestjs/core or by having some libraries locally linked.

It does not seem to be related to nestjs-cls.

AvnerMaster commented 11 months ago

You are right, I had other libraries that were linked. I managed to fix it by changing the nestjs libraries versions to match my linked libraries nestjs libraries versions. Thank you

Papooch commented 11 months ago

I'm glad it's resolved. I'm going to assume that a solution to OP's problem is also similar to yours and close this issue.