Giter Site home page Giter Site logo

Unable to import about kafkajs-lz4 HOT 6 OPEN

indix avatar indix commented on September 21, 2024
Unable to import

from kafkajs-lz4.

Comments (6)

VladC12 avatar VladC12 commented on September 21, 2024 1

I have the same problem, I'm getting TypeError: LZ4Codec is not a constructor.
This was on the beta with: npm install [email protected]
The full error:

 TypeError: LZ4Codec is not a constructor
     at Object.<anonymous> (/xxx/public/server/controller/kafka.js:13:21)
     at Module._compile (node:internal/modules/cjs/loader:1116:14)
     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1169:10)
     at Module.load (node:internal/modules/cjs/loader:988:32)
     at Module._load (node:internal/modules/cjs/loader:829:12)
     at Function.c._load (node:electron/js2c/asar_bundle:5:13343)
     at Module.require (node:internal/modules/cjs/loader:1012:19)
     at require (node:internal/modules/cjs/helpers:102:18)
     at Object.<anonymous> (/xxx/public/server/controller/consumer.js:1:15)
     at Module._compile (node:internal/modules/cjs/loader:1116:14)
 npm run electron:start exited with code 7

I'm doing an electron app.

On the server side I have kafka.js file which I use, with the code:

const { Kafka, CompressionTypes, CompressionCodecs } = require("kafkajs");
const { LZ4Codec } = require('kafkajs-lz4');

const username = "nope";
const password = "nope";
const sasl =
  username && password ? { username, password, mechanism: "plain" } : null;
const ssl = !!sasl;

// LZ4 compression
const LZ4Instance = new LZ4Codec()
CompressionCodecs[CompressionTypes.LZ4] = LZ4Instance.codec;

const kafka = new Kafka({
  clientId: "noper",
  brokers: ["nope"], 
  ssl,
  sasl,
});

module.exports = kafka;

Then consumer.js:

const kafka = require("./kafka");
const io = require("../socket");
const consumer = async (topic) => {
  const consumer = kafka.consumer({ groupId: "test-group" });

  try {
    await consumer.connect();
  } catch (e) {
    console.log("Error: couldn't connect to Kafka");
    return;
  }
  console.log("Connected to Kafka!");
  try {
    await consumer.subscribe({ topic, topic });
  } catch (e) {
    console.log("Error: couldn't subscribe to topic.");
    return;
  }
  console.log("Kafka consumer subscribed.");
  console.log("Consuming...");

  try {
    await consumer.run({
      eachMessage: async ({ topic, partition, message }) => {
        console.log("Received message: " + message.value.toString());
        io.getIO().emit("events", {
          action: "consume",
          event: message.value.toString(),
        });
      },
    });
  } catch (e) {
    console.log("Couldn't consume message");
  }
};

module.exports = consumer;

I hope this helps narrow it down somehow. Ask if you need more specific info.

from kafkajs-lz4.

paambaati avatar paambaati commented on September 21, 2024

@link2malkeet Please provide a minimal, complete and verifiable example for your issue, as they're both valid exports, they can work side-by-side in CommonJS and ES6 environments, and the latest beta version has been verified to work.

To verify yourself —

  1. Clone this repo.
  2. Add this package itself as a dependency.
  3. Modify this line to import the package you just added.
    - import LZ4Codec from '../src/index';
    + import LZ4Codec from 'kafkajs-lz4';
  4. Run tests (you need Docker for this).
    yarn run test
    

from kafkajs-lz4.

link2malkeet avatar link2malkeet commented on September 21, 2024

HI @paambaati
I will try again later

Just to summarize what I know or understand, the main index file works with default exports only, we don't need to define module.exports in typescript.

Thanks

from kafkajs-lz4.

AlanKavanagh avatar AlanKavanagh commented on September 21, 2024

Hi @paambaati Just following up on this, is the beta version going to be promoted?

from kafkajs-lz4.

paambaati avatar paambaati commented on September 21, 2024

@AlanKavanagh Can you confirm the beta version works for you? It'd be great if you can tell us a little about your Kafka cluster and how long you've been using the beta version.

from kafkajs-lz4.

harshagarwalsol avatar harshagarwalsol commented on September 21, 2024

I see below error as well:

TypeError: kafkajs_lz4_1.default is not a constructor

when using:

import LZ4Codec from 'kafkajs-lz4';

from kafkajs-lz4.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.