Common local library for managing javascript functions used in function nodes

I've stumbled on this old page:
Design: Function Library node · node-red/node-red Wiki · GitHub

We have seen a growing number of users who are inserting JavaScript into global context so it can be readily reused amongst the Function nodes in a flow. This is not a great developer experience and something we could improve.

Has there been any development around this since then? Am I overseeing another functionality that solves this problem? are Modules in the function node usable for managing my custom js code?

Yes, you can now load packages direct to a function node.

Does this mean I have to publish my package to NPM to use this? Or can I use this mechanism also for local packages? After all, the purpose is to have an utility library of javascript functions for own use, not something that is of any value for other users.

Hi @psmtvdw

The item you link to has not had anything done; the idea of having a reusable set of functions that can be easily reused.

Since then, as @TotallyInformation says, we have made it easier to use npm modules in the function node - but that is a different use case and does require modules to have been published to npm.

I wonder if that might be a future upgrade? To allow any specifier for a package that is supported by npm? Then things could be installed from GitHub and even locally.

I can't try it at the moment, but if a package is installed manually from GitHub or file, can it then be used in a function even though it has not been published?

1 Like

I've tested this, and the answer is yes (Tried both installing from local file and from a git repository). Manually updated the package file, ran npm install, add a module in a function node and it worked. It is an alternative to the approach of adding to the settings.js, but it doesn't really add much, except for a package file in the module where you can set a version number. You still need to manage your js functions outside node-red, hard to know from within the node-red editor which functions are available and what they do .

You can also save code to the function library and import it into a function node.

Indeed, but this is not really the solution to the problem described above. This is useful in case you want to reuse a complete function node. But function nodes loaded from this library earlier will not be updated after updating the function in the library I suppose. In my opinion the current open and save to library functions are not really a library but more of a collection of snippets/examples. In case of a real library, all java script functions used in every function node will be updated after updating the source code, just like you would use a module.