# Include external code in function

**URL:** <https://discourse.nodered.org/t/include-external-code-in-function/98704>\
**Category:** Developing Nodes\
**Created:** [17 August 2025 07:32 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704 "2025-08-17T07:32:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![manjha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/manjha/32/29301_2.png) [@manjha](https://discourse.nodered.org/u/manjha)\
**Post date:** [17 August 2025 07:32 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704/1 "2025-08-17T07:32:26Z")

</div>

In many of my function nodes, I use more-or-less standard subroutines. In stead of copy/paste these blocks of code every time I create a new function node, I would like to include them from a library. Is this possible?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [17 August 2025 07:37 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704/2 "2025-08-17T07:37:55Z")

</div>

Hi @manjha

Do you mean you have a set of subroutines, that is then duplicated in other function Nodes, where you need them in said function nodes?

---

<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:** [17 August 2025 08:11 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704/3 "2025-08-17T08:11:40Z")

</div>

There are a few approaches.

1. You could store your functions in flow or global context in 1 "master function" node then call them from your other function nodes
2. You could require a js file containing your functions via the settings.js file
3. You could do this the "node-red way"/visual flow way by creating subroutines using link-call
4. You could encapsulate them in a subflow

Imo, 3 is the best followed by 1. 2 is a chew on and requires restarts if you need to make a change. 4 generates LOTS of nodes and bloat in the runtime.

---

<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:** [17 August 2025 12:37 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704/4 "2025-08-17T12:37:40Z")

</div>

Instead of 2, you could also create a plugin. I do this with UIBUILDER in order to create shared code functions. There are also a few uibuilder function extensions made available to function nodes.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [17 August 2025 13:35 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704/5 "2025-08-17T13:35:28Z")

</div>

Thanks @Steve-Mcl!  
I was familiar with options 2-4, but not aware that you can store functions in context vars. Turns out you can also send functions as msg properties (to server nodes only, not to dashboard widgets).  
This is super cool. It allows to design generic (base-class) nodes which receive both dynamic data & dynamic methods ("polymorphism").

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [17 August 2025 15:23 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704/6 "2025-08-17T15:23:53Z")

</div>

There is a discussion about pros and cons of storing functions in context variables at [https://discourse.nodered.org/t/first-flow-on-all-my-rpis/88878](https://discourse.nodered.org/t/first-flow-on-all-my-rpis/88878)

---

<div class="post-metadata">

**Author:** ![manjha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/manjha/32/29301_2.png) [@manjha](https://discourse.nodered.org/u/manjha)\
**Post date:** [24 August 2025 09:40 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704/7 "2025-08-24T09:40:58Z")

</div>

Interesting, thanks for your reply!

Could you provide an example for option 3?

---

<div class="post-metadata">

**Author:** ![manjha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/manjha/32/29301_2.png) [@manjha](https://discourse.nodered.org/u/manjha)\
**Post date:** [24 August 2025 09:44 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704/8 "2025-08-24T09:44:52Z")

</div>

Yes, that’s it. For instance, for storing info into flatfile logs, I use a standardized date/time as part of the record. This is a small subroutine that I copy to function nodes when I need them. This would be a candidate.

Another example is setting up a command for Domoticz, the subroutine expects the IDX and data, and returns the formatted command that can be returned and given to an MQTT-out node.

---

<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:** [24 August 2025 09:48 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704/9 "2025-08-24T09:48:32Z")

</div>

> [@manjha](#):
>
> Could you provide an example for option 3?

There are hundreds of examples all over this forum.

- [Load test with hundreds of JSON messages - #13 by Steve-Mcl](https://discourse.nodered.org/t/load-test-with-hundreds-of-json-messages/97410/13)
- [How to process multiple data - #7 by Steve-Mcl](https://discourse.nodered.org/t/how-to-process-multiple-data/58367/7)
- [More...](https://discourse.nodered.org/search?q=link-call)

There is a built in importable demo too  
CTRL-I

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f9ad25e3b02b4ea36535b54be44751819976c455.png)

---

<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:** [23 October 2025 09:49 UTC](https://discourse.nodered.org/t/include-external-code-in-function/98704/10 "2025-10-23T09:49:01Z")

</div>

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