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

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:

function uibDeepFind(obj, matcher, cb) {
  if (matcher(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.

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.

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.

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 :wink:

1 Like

Well, that was my first thought. :slight_smile: Naughty but nice. :rofl:

Can't you do this in settings.js?

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.

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

'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:

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.


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.

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