How to execute a nodejs file inside function node


Sorry if it's a stupid question, I am new to node-red.

Trying to execute this code inside a function node:

"use strict";

const { KafkaStreams } = require("./../index.js");
const { nativeConfig: config } = require("./../test/test-config.js");

const kafkaStreams = new KafkaStreams(config);
const stream = kafkaStreams.getKStream("test12345");

kafkaStreams.on("error", (error) => {
    console.log("Error occured:", error.message);


    .filter((rec) => rec != null)
    .tap((msg) => console.log(msg.node.ipaddr))
    .to("output-topic-12", 1, "buffer");


What do I need to change in order to be able to execute this code inside a function node?
I was able to do it via a exec node, but want to set certain parameters inside the code and eventually create a kafka streams node.

Before implementing it yourself in a function node as code, I would take a look in the Flow Library for existing nodes to your problem. That way it can be integrated much better into a flow-based environment like Node-RED. :grinning:

As it turns out, there are some Kafka nodes available:

Aside from the good point that @kuema makes about checking for existing nodes, on the more specific point about the Function node, I recommend reading the docs

In particular, the section on loading additional modules as you cannot use require in the Function node directly -


And, if you really wanted to implement it yourself inside a Function node, error handling becomes really important. For example, an unhandled error emitted by a stream (or any EventEmitter) will crash the whole NodeJS process! :boom:

Thanks! There is a lot of kafka nodes already, but unfortunetly no kafka streams node. I will keep in mind the error handling :slight_smile:

Well, you can always implement one that can. Or, even better, take one of the existing ones and extend it. It isn't that hard if you already know your way around NodeJS. :slightly_smiling_face:

After all, doing this kind of reusable "low-level" stuff in a custom node makes it much easier to use in Node-RED and improves the ecosystem around it as well.