BestPractice to manage/write JS (function-node) code outside Node-Red?

Hi,
as my Node-Red is growing and growing,
more function nodes I am starting to use.

As I am using VSCode and GitLab allready my Question is:

Is there a better way of managing my function nodes than copy/paste between
Node-Red and VSC each time I do changes?

best regards
Antonio B.

(I allready thought about using functions in global context, but I am afraid the time is eaten up my life-time as I am very often stumble/fall on the higher level of programming, I eaven thought about creating my own nodes, but ... time AND knowledge is limited on my side :grin:)

Remind: I am using all the stuff as a hobby and not as a pro :laughing:

Well seeing as how from V3.0 onwards, the function node uses the same editor as VSCode, I wonder why you are doing this in the first place?

Also, are you writing function nodes for "most things"?

My best practice/advise would be always fall back to function nodes as a last resort. This trains you to think about breaking problems down into graphical units, furthering your knowledge of FBP and creating more visual/self documenting flows.


Perhaps you could share some examples of smaller and larger function nodes?

While this is technically true Steve, the user experience can be VERY different. Since the web editor has a relatively vanilla configuration whereas a local VScode installation may have lots of personalised settings, different settings for different folders/projects/workspaces, use multiple monitors, have many 3rd-party extensions - not all of which work on the web version.

This assumes certain workflows and use-cases. While I would generally agree with you, there may be some reasons for doing things differently (well more probably but these are the 4 most common for me anyway):

  1. Your flow gets more complex and has many - too many - nodes to allow the flow to be easily comprehended. This may especially happen with a lot of branching. It these cases, it may be better to re-engineer the "prototype" node-based flow into a smaller number of function nodes.

  2. Your required processing is very complex at the outset and more easily described in words than pictures. In this case, if the editor's JavaScript is up to the task, it can be a lot easier to express as code rather than visually as nodes.

  3. You want to use Node-RED as a code-running framework. This is absolutely a valid use of Node-RED and I've done it myself. Node-RED gets me a server and some handy helpers (e.g. reading files, etc) and then lets me focus on just writing the logic using a function node.

  4. You have a team of developers sat on their hands and want them to contribute actively.

There was a node that allowed you to use a file instead of in-flow code but I don't think it has been updated for a long while and I'm not sure whether it still works.

You could also write your own Node.js module and assign it to a global in settings.js - though that requires you to reload node-red after each change. Though the module could be made to dynamically load from other files. But you still have to global.get the library in each function node.

Or you could take the extreme measure of creating your own custom node that loaded scripts from a known server location and executed them. More up-front work of course but would give you absolute control. You could specify common libraries, enforce (or not) VM use for security (or speed), etc.

@daspixel

Have you tried wrapping your function in a subflow? I think it will allow you to manage 1 copy of the function that appears in the multiple subflow instances.