# How to execute a nodejs file inside function node

**URL:** <https://discourse.nodered.org/t/how-to-execute-a-nodejs-file-inside-function-node/16821>\
**Category:** General\
**Created:** [17 October 2019 14:29 UTC](https://discourse.nodered.org/t/how-to-execute-a-nodejs-file-inside-function-node/16821 "2019-10-17T14:29:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![renzel](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@renzel](https://discourse.nodered.org/u/renzel)\
**Post date:** [17 October 2019 14:29 UTC](https://discourse.nodered.org/t/how-to-execute-a-nodejs-file-inside-function-node/16821/1 "2019-10-17T14:29:51Z")

</div>

Hi!

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

Trying to execute this code inside a function node:

```auto
"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);
});

console.log("Started");

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

stream.start();

```

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.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [17 October 2019 15:35 UTC](https://discourse.nodered.org/t/how-to-execute-a-nodejs-file-inside-function-node/16821/2 "2019-10-17T15:35:14Z")

</div>

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

As it turns out, there are some Kafka nodes available: [https://flows.nodered.org/search?term=Kafka](https://flows.nodered.org/search?term=Kafka)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 October 2019 15:41 UTC](https://discourse.nodered.org/t/how-to-execute-a-nodejs-file-inside-function-node/16821/3 "2019-10-17T15:41:13Z")

</div>

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 [https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions)

In particular, the section on loading additional modules as you cannot use `require` in the Function node directly - [https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules)

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [17 October 2019 15:46 UTC](https://discourse.nodered.org/t/how-to-execute-a-nodejs-file-inside-function-node/16821/4 "2019-10-17T15:46:48Z")

</div>

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! 💥

---

<div class="post-metadata">

**Author:** ![renzel](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@renzel](https://discourse.nodered.org/u/renzel)\
**Post date:** [17 October 2019 16:17 UTC](https://discourse.nodered.org/t/how-to-execute-a-nodejs-file-inside-function-node/16821/5 "2019-10-17T16:17:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [17 October 2019 16:33 UTC](https://discourse.nodered.org/t/how-to-execute-a-nodejs-file-inside-function-node/16821/6 "2019-10-17T16:33:54Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![donpedro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/donpedro/32/78837_2.png) [@donpedro](https://discourse.nodered.org/u/donpedro)\
**Post date:** [18 December 2023 13:58 UTC](https://discourse.nodered.org/t/how-to-execute-a-nodejs-file-inside-function-node/16821/7 "2023-12-18T13:58:03Z")

</div>

This is on a slightly different topic, but I ran across this thread when looking for people who had tried what I was about to try: node streams in Node-RED. I didn't really find anything more on it, so I've done my own thing; I've just published my first nodes so I guess I'm now in very early beta, but I'd love some input!

The existing project is [gulpetl](http://gulpetl.com/), and we're adding Node-RED capabilities to a project already built around streams. It's for ETL but not Kafka, but we're doing some of the same type of things. Anyway, we're headed toward 'reusable "low-level" stuff in a custom node', so hopefully we'll be improving the ecosystem a little bit. 😀 But given what @kuema said, I feel like I must have missed some nodes that are using streams. Are there really some more out there?

Anyway, here's the new [Node-RED section](https://docs.gulpetl.com/node-red) of our docs, with Getting Started and what-not. I hope to get a brief tour finished up in the next couple of days.
