Developing a project involving many flows, I needed to create function libraries, function arrays, and singleton objects (with data and methods), shared and accessible from each node/flow.
The 'standard' solution (import etc.) doesn't excite me.
I found a solution that works best for me: it uses singleton objects, and this is a good replacement for shared libraries, having the following advantages, also keeping the distribution issues in mind:
- Easy to implement.
- Easy to debug.
- Visible in node-red 'Context data' debug pad.
- No extra files: all in JSON import/export.
- No config file editing, auto-installation.
An implementation can be seen here: ‘Meteo Utils’ functions library.
In short, place in 'On Start' of some function node, in your flow or in one flow of your project:
global.set("a_sing_obj", {}); // only data, empty in this example
context.global.a_sing_obj = global.get("a_sing_obj");
Then add methods, like:
context.global.a_sing_obj.one_method= function(....){
// more code, you can use 'this.' here
}
done: then you can do calls like:
context.global.a_sing_obj.one_method(...)
in any node and in any flow or subflow.