Well clearly, in order to do something actively, the code has to be run. So if you want to deliver something dynamic, you will need to attach it to Node-RED somehow:
- In an existing node as an API. That node does not actually need to be added to your flow Since the act of enabling the node via the palette is enough to load the module which will instanciate the API.
- Loaded to a global in your settings.js - not tried this but it may be possible to access that from a node's config html file.
There are a few other possibilities that I can think of.
In uibuilder, for example, I now have several modules that are singleton classes. That is where the class is instanciated by the module iteself so that there can only ever be a single instanciation of the class. So that instanciated class (because Node-RED is the host, not your node) is now accessible to ANY node's runtime module. Though you do have to take a little care about order of loading. I instanciate the class at the start of my node's runtime module but I don't actually configure it until a bit later on. This makes use of how node.js loads modules. A module is only actually loaded once no matter how many times you
Take a look at the source code for uibuilder. Especially, have a look at the v4.2.0 branch code because that has been reworked into a structure that is much easier to follow.
So in the future, someone could write a node that made use of one of the classes as long as they made sure to check that the class instance is actually configured (I provide a gettable flag for that). This would allow another node to make use of the socket.io comms that uibuilder uses for example.
In theory, you might even be able to use something like that approach but put the initial require in settings.js which would be called even earlier and wouldn't require a node to be in the palette. Not tried that though but I think it should work. The downside of this would be that you would not have any access to any of the Node-RED features since you would be running the class instance before any of the node-red specific code was active.
In addition to the singleton approach, depending on how your code works, you might be able to get away with static methods and variables in a class instead.