# Is it possible to somehow refer to a function from the library of personal functions in the code of the node "function"?

**URL:** <https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329>\
**Category:** General\
**Tags:** function-node\
**Created:** [16 December 2022 16:21 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329 "2022-12-16T16:21:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [16 December 2022 16:21 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/1 "2022-12-16T16:21:40Z")

</div>

Hello  
Is it possible to somehow refer to a function from the library of personal functions in the code of the node "function" ?

---

<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:** [16 December 2022 16:36 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/2 "2022-12-16T16:36:49Z")

</div>

Yes.

Store your functions in an object then store that in global or flow scope

```auto
const utilityFunctions = {
   myFunction: (x,y) {
     return x + y
   }
}
global.set('utilityFunctions', utilityFunctions)

```

Then in the other functions, get that back out of global scope...

```auto
const utilityFunctions = global.get('utilityFunctions')
msg.payload = utilityFunctions.myFunction(2,2) // 4
return msg

```

* * *

However, this is hiding the beauty of node-red.

Really, a better way would be to make each shared function (that you want to re-use) a function node and use the `link-call` node to call them. This makes (kinda) visual subroutines that you re-use.

---

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [16 December 2022 17:24 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/3 "2022-12-16T17:24:45Z")

</div>

> [@Steve-Mcl](#):
>
> However, this is hiding the beauty of node-red.
> 
> Really, a better way would be to make each shared function (that you want to re-use) a function node and use the `link-call` node to call them. This makes (kinda) visual subroutines that you re-use.

I absolutely agree with you.  
But in my case, about a hundred nodes are already used, several of which contain more than 200 lines of code, and one even more than 1000.

Thank you very much for your help !

---

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [16 December 2022 17:36 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/4 "2022-12-16T17:36:50Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> const utilityFunctions = {
> myFunction: (x,y) {
> return x + y
> }
> }
> 
> ```

The litlle problem:

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/358b657e61abba8d3ce6894644ed08ac8607ec8f.jpeg)

---

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [16 December 2022 17:46 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/5 "2022-12-16T17:46:09Z")

</div>

> [@Steve-Mcl](#):
>
> Store your functions in an object then store that in global or flow scope

The error turned out to be a typo:  
there was:  
`myFunction: (x, y) {`  
need to:  
`myFunction: function(x, y) {`

```auto
const utilityFunctions = {
    myFunction: function(x, y) {
        return x + y
    }
}
global.set('utilityFunctions', utilityFunctions)

//Then in the other functions, get that back out of global scope...
const utilityFunctions2 = global.get('utilityFunctions')
msg.payload = utilityFunctions2.myFunction(2, 2) // 4
node.warn('msg.payload =' + msg.payload)

return msg

```

---

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [16 December 2022 17:55 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/6 "2022-12-16T17:55:14Z")

</div>

[Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)

And why didn't you use ';' at the end of lines in your code ?

---

<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:** [16 December 2022 18:30 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/7 "2022-12-16T18:30:28Z")

</div>

> [@Yury](#):
>
> And why didn't you use ';' at the end of lines

Habit. Personal preference.

I try to stick to eslint standard config

---

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [16 December 2022 18:50 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/8 "2022-12-16T18:50:19Z")

</div>

> [@Steve-Mcl](#):
>
> eslint standard config

What is this "eslint" ?

---

<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:** [16 December 2022 18:56 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/9 "2022-12-16T18:56:02Z")

</div>

It is a coding style.

[https://standardjs.com/rules.html](https://standardjs.com/rules.html)

Currently, node-red function node does not directly support eslint like Vs code does but I am now pretty used to using eslint that I tend to write the same style in node-red functions.

There is a separate linter you can install for node-red if you want code linting

---

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [16 December 2022 19:07 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/10 "2022-12-16T19:07:37Z")

</div>

[Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)  
It's very intresting information for me.  
And what is the name of the coding style that is used in the "function" Node RED 3.1 nodes ?

---

<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:** [16 December 2022 21:17 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/11 "2022-12-16T21:17:36Z")

</div>

I believe that node-red core coding tries to stick to JavaScript Standard but I'm not sure what, if any eslint settings have been used for the node-red Monaco editor element. I think that it tries to remain relatively neutral - so for example you can use `;` or not or even a mixture and it doesn't complain. That is probably sensible for the range of people likely to use node-red.

---

<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:** [30 December 2022 21:17 UTC](https://discourse.nodered.org/t/is-it-possible-to-somehow-refer-to-a-function-from-the-library-of-personal-functions-in-the-code-of-the-node-function/72329/12 "2022-12-30T21:17:52Z")

</div>

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