Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to import #8

Open
link2malkeet opened this issue Dec 7, 2020 · 6 comments
Open

Unable to import #8

link2malkeet opened this issue Dec 7, 2020 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@link2malkeet
Copy link

Seems you have mixed the default import vs module.export.
export default class LZ4Codec
vs
module.exports = LZ4Codec;
So unable to use the library.
Can we please fixed it?

@paambaati paambaati self-assigned this Dec 8, 2020
@paambaati paambaati added the question Further information is requested label Dec 8, 2020
@paambaati
Copy link
Collaborator

@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
    

@paambaati paambaati changed the title Imports are not properly defined. Unable to import Dec 8, 2020
@link2malkeet
Copy link
Author

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

@AlanKavanagh
Copy link

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

@paambaati
Copy link
Collaborator

@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.

@VladC12
Copy link

VladC12 commented May 23, 2022

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.

@harshagarwalsol
Copy link

harshagarwalsol commented Mar 22, 2024

I see below error as well:

TypeError: kafkajs_lz4_1.default is not a constructor

when using:

import LZ4Codec from 'kafkajs-lz4';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants