How can I share global ClsModule between my NodeJS app and a package I've installed #240
-
Hi, I am initiating the import { ClsModule } from 'nestjs-cls'
import { MyCustomModule } from 'my-custom-lib'
@Module({
imports: [
ClsModule.forRoot({
global: true,
interceptor: { mount: true },
}),
MyCustomModule.forRoot({
isGlobal: true,
}),
]
})
export class AppModule {} And now I have another package (published as npm lib) which is injecting the /usr/src/app/node_modules/my-custom-lib/dist/request-utils/correlation-id.service.js:20
return this.clsService.get("some-key");
^
TypeError: Cannot read properties of undefined (reading 'get')
at CorrelationIdService.correlationId (/usr/src/app/node_modules/my-custom-lib/dist/request-utils/correlation-id.service.js:20:40)
at Format.transform (/usr/src/app/node_modules/my-custom-lib/dist/winston/winston.factory.js:21:51)
at Format.transform (/usr/src/app/node_modules/logform/combine.js:20:24)
at DerivedLogger._transform (/usr/src/app/node_modules/winston/lib/winston/logger.js:314:29)
at DerivedLogger.Transform._read (/usr/src/app/node_modules/readable-stream/lib/_stream_transform.js:166:10)
at DerivedLogger.Transform._write (/usr/src/app/node_modules/readable-stream/lib/_stream_transform.js:155:83)
at doWrite (/usr/src/app/node_modules/readable-stream/lib/_stream_writable.js:390:139)
at writeOrBuffer (/usr/src/app/node_modules/readable-stream/lib/_stream_writable.js:381:5)
at DerivedLogger.Writable.write (/usr/src/app/node_modules/readable-stream/lib/_stream_writable.js:302:11)
at DerivedLogger.log (/usr/src/app/node_modules/winston/lib/winston/logger.js:253:14) So I know if I define another I guess I can send the import { ClsModule } from 'nestjs-cls'
import { MyCustomModule } from 'my-custom-lib'
@Module({
imports: [
ClsModule.forRoot({
global: true,
interceptor: { mount: true },
}),
MyCustomModule.forRoot({
isGlobal: true,
providers: [ClsService], // But this does not work
}),
]
})
export class AppModule {} The error message is this: Error: Cannot create ClsService because no AsyncLocalStorage instance was provided.
Please make sure that ClsService is only provided by the ClsModule and not constructed manually or added to the providers array.
at new ClsService (/usr/src/app/node_modules/nestjs-cls/dist/src/lib/cls.service.js:35:19)
at Injector.instantiateClass (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:365:19)
at callback (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:65:45)
at async Injector.resolveConstructorParams (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:144:24)
at async Injector.loadInstance (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:70:13)
at async Injector.loadProvider (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:97:9)
at async /usr/src/app/node_modules/@nestjs/core/injector/instance-loader.js:56:13
at async Promise.all (index 7)
at async InstanceLoader.createInstancesOfProviders (/usr/src/app/node_modules/@nestjs/core/injector/instance-loader.js:55:9)
at async /usr/src/app/node_modules/@nestjs/core/injector/instance-loader.js:40:13 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 18 replies
-
I guess I've found the answer, but I would like to confirm that the
|
Beta Was this translation helpful? Give feedback.
@kasir-barati Ah, so I cloned your project and found the problem. You're missing the flag
emitDecoratorMetadata
in your shared library'stsconfig.json
.That means a built source of the
CorrelationIdInterceptor
doesn't contain any metadata that NestJS can read to inject dependencies, so it doesn't inject anything, and therefore stuff is undefined at runtime.Once I added that flag and rebuilt the library, it works as intended. An alternative to enabling that flah is to explicitly decorate the constructor parameter with
@Inject(ClsService)
.