Function-node backend as reusable package

Hi folks,

I am currently in a situation where I need a Function-node alike VM in the backend of one of my nodes, to implement a useful feature.

For my node-red-contrib-blockly node I needed something similar at the time being, so I had worked around it by copying the js file from the Function-node and then I applied some small adaptations (like e.g. the name of my node).

But that way of working has a lot of disadvantages:

  • My node gets out of sync with the original Function node (API changes, bug fixes, ...)
  • Every time I want to get in sync again I need to redo all my small changes again.
  • My own custom code is becoming hard to find in my node, because my node is polluted with a lot of copied code.
  • ...

For all those reasons, I am not going that road again. So unfortunately I cannot implement my new feature in my other node at the moment.

It would be nice if the Function-node backend code could be easily reused somehow in custom nodes. For example via an npm package dependency or something else.

Thanks for taking this into consideration!!

So the core processing needs to be extracted into a module. Perhaps @knolleary might wade in with a comment as to whether that would be an acceptable contribution change.

Once done, it should be possible to reuse the module in other nodes?

Hi Julian,

Yes indeed it would be nice if the Function-node backend could be extracted into a separate module. Then the Function node could us that module, and custom nodes also. When e.g. the API is extended (to allow the javascript code to call extra features from Node-RED), then the custom node could automatically offer the same feature automatically.

I understand that it is not possible to make the backend of the core nodes available as a separate module, but the Function-node is a bit special for me. It allows people to execute javascript code with limited capabilities, which can be useful in multiple use cases.

Don't know whether a separate npm package would be the best solution. Perhaps that is not the standard way of working in the core. So I haven't thought about the possible implementations yet...

I haven’t looked at the code, but it sounds like you need it to be structured as a class. Then, you could create your own class extending the base class and add/change any of its features without affecting the base class.

1 Like

I don't think it should be a separate package, I think that a reusable library should be fine? Not sure about module isolation though and whether you might be blocked from accessing a core library module from your own package - I'm never too sure about that.

That would also be my first choice and I've done that in uibuilder, moving library code to their own classes in their own modules. Though in that case, I've actually exported the instance of the class rather than the class itself because I only ever want a single instance no matter how many uibuilder nodes you add. Would have to look at the core code to try and work out what would be best here.

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