NestJS与共享模块的问题



我正在尝试在NestJS中创建一个可以在多个微服务之间共享的共享日志模块。

日志模块作为微服务的一部分时可以工作,但是当我将代码提取到它自己的NPM模块时,它就不再工作了。

下面是我分享的NPM模块代码示例:

// my-logger.module.ts

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { LoggerModule } from 'nestjs-pino';
@Module({
imports: [
LoggerModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
pinoHttp: {
level: process.env.LOG_LEVEL || 'info',
redact: configService.get<string[]>('logger.redacted.fields'),
prettyPrint: {
colorize: false,
singleLine: true,
levelFirst: false,
translateTime: "yyyy-mm-dd'T'HH:MM:ss.l'Z'",
messageFormat: '{req.headers.x-correlation-id} [{context}] {msg}',
ignore: 'pid,hostname,context,req,res,responseTime',
errorLikeObjectKeys: ['err', 'error'],
},
},
}),
inject: [ConfigService],
}),
],
controllers: [],
providers: [],
})
export class MyLoggerModule {}

下面是我的NestJS微服务的App模块示例

// app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import configuration from '../config/configuration';
import { MyLoggerModule } from '@my-company/my-logger.module';
import { HttpModule } from '@nestjs/axios';
@Module({
imports: [
ConfigModule.forRoot({ load: [configuration] }),
MyLoggerModule,
HttpModule,
],
controllers: [],
providers: [],
})
export class AppModule {}

微服务通过共享的npm模块正确构建和部署。但是,每次我发送一个新请求时,它都会导致服务重新启动,并出现以下错误:

node[1]: ../src/tcp_wrap.cc:149:static void node::TCPWrap::New(const v8::FunctionCallbackInfo<v8::Value>&): Assertion `args[0]->IsInt32()' failed.

有没有人知道为什么这是失败的?

注意:我猜它与ConfigService/ConfigModule在两个模块中使用有关。然而,我不明白为什么相同的代码工作时,它是微服务的一部分

我们在尝试配置共享模块时遇到了类似的问题,并遇到了一大堆问题。根本原因是包版本不一致。这个答案给我指明了正确的方向。

总之,仔细检查应用程序和共享包的所有包版本号。

最新更新