Strategy for managing lots of functions and js code

As my Home Assistant Node-RED project is becoming bigger, I am looking for better ways to manage my flows and, in particular, the javascript from my function nodes. I'm impressed that Node-REDs javascript enables classes, arrow functions, promises and more (but apparently is still using require rather than import and export?).

node-red-contrib-flow-manager does exactly what I want for flows. But I still find how I manage my js code to be a bit awkward.

Code inside of json or YAML flow files is one awkwardness. A bigger one is where to put, and how to include, common code and large class definitions. [At present I'm attaching my common functions as properties of an object that I set on the global object (no code completion when I use these functions), and in-lining class definitions within the functions where they are used.]

Maybe there are better existing code management strategies that I don't know about? One is to attach common code to settings.js but that doesn't seem to make it any cleaner, and then forces a node-red restart if you change the code (?). Admittedly I haven't investigated creating custom nodes for every one of my function nodes, as that seems a bit extreme.

It would be nice if the functions' js code was split out into separate files, rather than all being inside flow files. This would be better for

  • editing purposes (use vs-code, even though the built-in editor is good),
  • version control
  • and possibly:
    • would allow importing common code (via export and import) that could be imported at deploy time.
    • code generation from TypeScript
    • an easier debugging setup?

Any thoughts or suggestions?

Sadly, Node.js has seriously messed up the transition from CommonJS to ES Modules. So it is extremely hard to transition.

You can create your own code module or require in settings.js so tht it is available on the global object.

I would be a little cautious about using node-red-contrib-flow-manager. I note that there are unaddressed issues against the repository so it is not clear whether the node is maintained.
I also note that when I install it I see

npm WARN deprecated date-format@0.0.0: 0.x is no longer supported. Please upgrade to 4.x or higher.
npm WARN deprecated axios@0.19.2: Critical security vulnerability fixed in v0.21.1. For more information, see https://github.com/axios/axios/pull/3410
npm WARN deprecated log4js@1.0.1: 1.x is no longer supported. Please upgrade to 6.x or higher.
npm WARN deprecated streamroller@0.2.2: 0.x is no longer supported. Please upgrade to 3.x or higher.

In particular the fact that it is pulling in a module with a critical security vulnerability is concerning.

Also make sure you know how to get back from multiple partial flows files to a single one, in case the node stops working.

Thanks for this warning, which I will heed. I had just installed it, without since making any flow changes. So I've reverted to the single mega flows.json file.

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