# Is there a way for a custom node to add a function that is accessible in a function node?

**URL:** <https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530>\
**Category:** Developing Nodes\
**Created:** [1 July 2023 15:51 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530 "2023-07-01T15:51:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 July 2023 15:51 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530/1 "2023-07-01T15:51:56Z")

</div>

I would like to add an object deep find javascript function to Node-RED in such a way that it is accessible in a function node. Is there any reasonable way to achieve this?

The deep find function is a recursive function that searches deep into an object and returns the sub-set of the object where(if) the search is true. Not especially efficient but is very flexible. Here is the function:

```auto
function uibDeepFind(obj, matcher, cb) {
  if (matcher(obj)) {
    cb(obj)
  }
  for (let key in obj) {
    if (typeof obj[key] === 'object') {
      uibDeepFind(obj[key], matcher, cb)
    }
  }
}

```

`matcher` and `cb` are, themselves, functions that you pass in.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 July 2023 16:02 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530/2 "2023-07-01T16:02:36Z")

</div>

As far as I know, you cannot add to the "things" a function node has access to. I am pretty sure this is not exposed for plugins or nodes to "add to"

Obviously you could add it to global context but it's not ideal.

The other way is to publish your code to npm and use the function node setup.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 July 2023 16:04 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530/3 "2023-07-01T16:04:59Z")

</div>

Thanks Steve. Publishing a package for 10 lines of code seems a little excessive though!

I could, of course abuse the prototype of some existing object but I really don't like doing that.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 July 2023 16:07 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530/4 "2023-07-01T16:07:21Z")

</div>

> [@TotallyInformation](#):
>
> I could, of course abuse the prototype of some existing object but I really don't like doing that

Not that I advocate such things but if you were to do this then I would suggest you abuse nicely e.g. `RED.util.uib.myFunction` 😉

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 July 2023 16:11 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530/5 "2023-07-01T16:11:10Z")

</div>

Well, that was my first thought. 🙂 Naughty but nice. 🤣

---

<div class="post-metadata">

**Author:** ![jmorris644](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jmorris644/32/31964_2.png) [@jmorris644](https://discourse.nodered.org/u/jmorris644)\
**Post date:** [2 July 2023 01:52 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530/6 "2023-07-02T01:52:40Z")

</div>

Can't you do this in settings.js?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 July 2023 09:55 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530/7 "2023-07-02T09:55:22Z")

</div>

Well, you can certainly add a function there to global. However, that would require everyone who wants to use it to add it themselves. What I wanted was a way to make something available to anyone who has installed uibuilder without having to make them do something extra. Rather like node-red plugins. But for function nodes.

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [2 July 2023 13:02 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530/8 "2023-07-02T13:02:12Z")

</div>

> [@TotallyInformation](#):
>
> Publishing a package for 10 lines of code seems a little excessive though!

Publishing a separate module just for helper functions might be tedious, but there is an alternative that seems to work.

In one of my published nodes, `@kevingodell/node-red-mp4frag`, I added an extra folder called `libs` containing a `helper.js` file:  
 ![Screen Shot 2023-07-02 at 7.45.25 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/353a6ba0d4acd417263a8de369585aa124cb2d6d.png)

```auto
'use strict';

module.exports = {
    myfunc: function(val) {
        console.log('hello', val);
    }
}

```

In the function node, I used the section for importing modules to point to the file in the sub folder `/libs/helper`:

 ![Screen Shot 2023-07-02 at 7.54.01 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/57327da6040bf215387748960fd9c4f4e8da3ac6.png)

The function was callable as `Helper.myfunc()` in the `On Message` section.

While it does require a little extra work for your end user to access it, it is definitely a lot better than having to edit the setting.js file.

---

<div class="post-metadata">

**Author:** ![jmorris644](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jmorris644/32/31964_2.png) [@jmorris644](https://discourse.nodered.org/u/jmorris644)\
**Post date:** [7 July 2023 13:22 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530/9 "2023-07-07T13:22:16Z")

</div>

I did a search and could not find it but I have a vague memory of someone that created a plugin shell that would actually use the content of a function node as its source.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 September 2023 13:22 UTC](https://discourse.nodered.org/t/is-there-a-way-for-a-custom-node-to-add-a-function-that-is-accessible-in-a-function-node/79530/10 "2023-09-05T13:22:34Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
