# Add js function available on every function node

**URL:** https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297
**Category:** General
**Created:** [4 January 2024 17:17 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297 "2024-01-04T17:17:00Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![hammeronthenet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hammeronthenet/32/29102_2.png) [@hammeronthenet](https://discourse.nodered.org/u/hammeronthenet)
#### Post date: [4 January 2024 17:17 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/1 "2024-01-04T17:17:00Z")

</div>

Hi all,  
as wrote in subject: there's a way (better if not inside flows but in settings or in a more "low level" way) to add fucntions (or prototype extension) so that these functions are available everywhere in every "functions" node?

For example: a function that extracts only numeric characters from a string or some simple utility.

Thank you in advance.

---

<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: [4 January 2024 17:27 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/2 "2024-01-04T17:27:48Z")

</div>

If you _must_ go down this (some would say "anti-pattern") path, then you can already make your own library of functions, store them in `global` or `flow` context, then you can access them in any function node you desire.

The more _pattern_ way is to break your solutions down into small nodes, pass it down the wire, update the msg, pass it on.

So the graphical way of doing what you ask is to use the `link-call` node to mass the `msg` through a re-useable flow (that can also contain utility functions) that then passes back the msg with any updates applied.

---

<div class="post-metadata">

### Author: ![hammeronthenet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hammeronthenet/32/29102_2.png) [@hammeronthenet](https://discourse.nodered.org/u/hammeronthenet)
#### Post date: [4 January 2024 19:33 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/3 "2024-01-04T19:33:14Z")

</div>

Hi Steve,  
Thank you for your time and for the answer.

Yes, that's absolutely true. But the scenario behind this question is really complicate to explain. I know the possibility to create functions inside global environment yet, but imho that's really weird. I'd prefer a solution in which I can extend the js with some simple functions focused on what I had to do (or better:focused on what the user had to do). Just lime the startup way on function node to load external libraries. That should be a good compromised.

---

<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: [4 January 2024 19:51 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/4 "2024-01-04T19:51:06Z")

</div>

It is _possible_ to do this with a custom node or (probably, not tested it) with a plugin. It isn't that hard to expose some functions into the function node VM's.

**HOWEVER** , that comes with a massive caveat. The method is not documented, might change, and you might one day find yourself with a name clash.

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [4 January 2024 23:03 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/5 "2024-01-04T23:03:05Z")

</div>

Use a _Immediately Invoked Function Expressions_ , _IIFE_

This is a trivial example. Somewhere in the Forum there is an explanation of how to create a \*.js file and load it into Node-RED.

```auto
const utility = (function () {

    /*
    * Functions Available
    *
    * isNumber,
    *
    */

 
    /**
    * Description Determines whether or not supplied argument is a number.
    *
    * @param {*} value - The argument to check.
    *
    * @return {boolean} - True if the argument is a number, otherwise false.
    */
    function isNumber(value) {
        return typeof value === 'number' && isFinite(value)

    } // End Function isNumber()

    return {

        isNumber,

    };

}())

global.set('utility', utility, 'file')

```

---

<div class="post-metadata">

### Author: ![hammeronthenet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hammeronthenet/32/29102_2.png) [@hammeronthenet](https://discourse.nodered.org/u/hammeronthenet)
#### Post date: [5 January 2024 14:33 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/6 "2024-01-05T14:33:03Z")

</div>

I'm really close to the solution.  
@Buckskin I see that possibility:

[https://flows.nodered.org/flow/195773d3b493d81c9bf012f64da02ea3](https://flows.nodered.org/flow/195773d3b493d81c9bf012f64da02ea3)

And I create inside /data a file with your example (without the global.set).

Then inside setting.js:

```auto
    functionGlobalContext: {
	      utility:require('./ysutils.js')
        // os:require('os'),
    },

```

Restarting nodered, everything works except that if I call inside a function node my function:

```auto
var utility = global.get('utility');
node.warn(utility);
return msg;

```

It returns "empty".

Uhm... why?

---

<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: [5 January 2024 14:43 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/7 "2024-01-05T14:43:03Z")

</div>

> [@hammeronthenet](#):
>
> ```auto
> var utility = global.get('utility');
> node.warn(utility);
> return msg;
> 
> ```
> 
> It returns "empty".
> 
> Uhm... why?

Hard to say without knowing what the required module looks like. It is exporting something I assume?

---

<div class="post-metadata">

### Author: ![hammeronthenet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hammeronthenet/32/29102_2.png) [@hammeronthenet](https://discourse.nodered.org/u/hammeronthenet)
#### Post date: [5 January 2024 14:44 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/8 "2024-01-05T14:44:32Z")

</div>

```auto
const utility = (function () {

    /*
    * Functions Available
    *
    * isNumber,
    *
    */

    /**
    * Description Determines whether or not supplied argument is a number.
    *
    * @param {*} value - The argument to check.
    *
    * @return {boolean} - True if the argument is a number, otherwise false.
    */
    function isNumber(value) {
        return typeof value === 'number' && isFinite(value)

    } // End Function isNumber()

    return {

        isNumber,

    };

}())

// global.set('utility', utility, 'file')

// module.export (utility);

```

This is the content of the file ysutility.js, inside /data.

I tried with "export" but with no chance

---

<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: [5 January 2024 14:46 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/9 "2024-01-05T14:46:52Z")

</div>

No, that is not a valid Node.js module and therefore the require will never return anything useful.

---

<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: [5 January 2024 14:50 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/10 "2024-01-05T14:50:12Z")

</div>

It should be something like:

```auto
module.exports {
    /**
    * Description Determines whether or not supplied argument is a number.
    * @param {*} value - The argument to check.
    * @return {boolean} - True if the argument is a number, otherwise false.
    */
    function isNumber(value) {
        return typeof value === 'number' && isFinite(value)
    } // End Function isNumber()
}

```

In your function node, it would look like

```auto
const {isNumber} = global.get('utility')
// ...

```

---

<div class="post-metadata">

### Author: ![hammeronthenet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hammeronthenet/32/29102_2.png) [@hammeronthenet](https://discourse.nodered.org/u/hammeronthenet)
#### Post date: [5 January 2024 14:53 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/11 "2024-01-05T14:53:24Z")

</div>

Thank you,  
I just arrived at the same solution 😃

Thank you @TotallyInformation !!!

You unlocked my brain 😃 😃 !

EDIT: just for completion, something like:

```auto
function isNumber(value) {
  return typeof value === 'number' && isFinite(value);
}

function isString(value) {
  return typeof value === 'string';
}

module.exports = {
  isNumber,
  isString
};

```

in my .js file, then inside settings.js:

```auto
    functionGlobalContext: {
	      utility:require('./ysutils.js')
        // os:require('os'),
    },

```

After restarting NR, I'm able to call functions inside my function node:

```auto
const utility = global.get('utility');
node.warn(utility);
return msg;

```

returns in debug view:

```auto
 object==>
          isNumer:function
          isString:function

```

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [5 January 2024 16:53 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/12 "2024-01-05T16:53:20Z")

</div>

Thank you - both of you. I had this in operation once but in the intervening years I had forgotten about it. (old age creeping in 😁)

---

<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: [19 January 2024 16:53 UTC](https://discourse.nodered.org/t/add-js-function-available-on-every-function-node/84297/13 "2024-01-19T16:53:33Z")

</div>

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