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?
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?
There are a few approaches.
- You could store your functions in flow or global context in 1 "master function" node then call them from your other function nodes
- You could require a js file containing your functions via the settings.js file
- You could do this the "node-red way"/visual flow way by creating subroutines using link-call
- 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.
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.
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").
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