错误 Nest.js 微服务,无法导入 RabbitMQ 的模块

时间:2021-03-01 10:55:10

标签: node.js module rabbitmq microservices nestjs

这里我有单独的模块代表 RabbitMQ 客户端模块

import { Module } from '@nestjs/common';
import { Transport, ClientsModule } from '@nestjs/microservices';
import { ServiceConfig } from '../config/service.config';
import { RmqSubscriberServiceToken } from '../../constants/token.constant';
import { ConfigModule } from '../config/config.module';

@Module({
  imports: [
    ClientsModule.registerAsync([
      {
        name: RmqSubscriberServiceToken,
        useFactory: (config: ServiceConfig) => ({
          transport: Transport.RMQ,
          options: {
            urls: [
              `amqp://${config.rabbitmq.User}:${config.rabbitmq.Password}@${config.rabbitmq.Host}`,
            ],
            queue: config.rabbitmq.QueueName,
            queueOptions: {
              durable: true,
            },
          },
        }),
        imports: [ConfigModule],
        inject: [ServiceConfig],
      },
    ]),
  ],
})
export class RmqSubscriberModule {}

当我将其导入我的服务模块时,出现以下错误

Error: Cannot find module '@nestjs/core/helpers/optional-require'
Require stack:
- /home/rharutyunyan/workspace/roulette/node_modules/@nestjs/microservices/nest-microservice.js
- /home/rharutyunyan/workspace/roulette/node_modules/@nestjs/microservices/index.js
- /home/rharutyunyan/workspace/roulette/common/lib/services/rabbitmq/rmq-subscriber.module.js
- /home/rharutyunyan/workspace/roulette/common/lib/services/rabbitmq/index.js

有人可以解释一下这里有什么问题吗?

1 个答案:

答案 0 :(得分:0)

发现问题,问题出在版本上,我的@nestjs/core版本和微服务不一样