javascripttypescriptrabbitmq

TypeScript error when using amqplib that can't


I'm working on a Node.js project using TypeScript and the amqplib package (currently at version 0.10.5). When I try to create a connection and channel in my RabbitMQ configuration file, TypeScript throws the following errors:

  1. Property 'close' does not exist on type 'Connection'
  2. Property 'createChannel' does not exist on type 'Connection'
  3. Type 'ChannelModel' is missing the following properties from type 'Connection': serverProperties, expectSocketClose, sentSinceLastCheck, recvSinceLastCheck, sendMessage
import * as amqp from 'amqplib';
import { Connection, Channel } from 'amqplib';

export class RabbitMQConfig {
  private static connection: Connection | null = null;
  private static channel: Channel | null = null;

  public static async connect(): Promise<void> {
    if (!this.connection) {
      this.connection = await amqp.connect('amqp://localhost');
      this.channel = await this.connection.createChannel();

      this.connection.on('close', () => {
        console.error('RabbitMQ connection closed.');
      });

      this.connection.on('error', (err) => {
        console.error('RabbitMQ connection error:', err);
      });

      console.log('RabbitMQ connected and channel created successfully.');
    }
  }

  public static getChannel(): Channel {
    if (!this.channel) {
      throw new Error('RabbitMQ channel is not initialized. Call connect() first.');
    }
    return this.channel;
  }

  public static async closeConnection(): Promise<void> {
    if (this.channel) {
      await this.channel.close();
      this.channel = null;
    }
    if (this.connection) {
      await this.connection.close();
      this.connection = null;
    }
    console.log('RabbitMQ connection closed.');
  }
}

I don't know what seems to be the issue since I did try importing the correct module but TypeScript still complains that the methods close() and createChannel() do not exist on the Connection type.

Any suggestions on how to resolve this type of issue would be greatly appreciated.


Solution

  • This might be a @types problem as I have just encountered the same or a similar problem updating the dependencies for a typescript project.

    Updating to "@types/amqplib": "0.10.7" with "amqplib": "0.10.5" broke the build with the errors you describe.

    I checked on the @types github repo in the ampqlib folder and there has been a recent commit which appears to remove the createChannel method, amongst others, from the Connection definition.

    I checked on the amqp-node/amqplib github repo in the change log, examples and also on the main readme page and they all indicate that createChannel etc are available on the Connection object.

    The solution in my case was to revert back to @types/amqplib v0.10.6

    "@types/amqplib": "0.10.6",
    

    Be sure to target the exact version (for now) i.e. don't use "^" otherwise you risk getting the later version.

    Having done this my project now builds