Best practice for sharing static object between instances of a node type

Where would one store a 'common object' for re-use across all node instances?

e.g. Lets say I am developing a node called image and on my flow there are 10 instances. I would like each instance (server side) to be able to access some shared object {fonts:[...]} that are initialised only once.
PS. I don't mean a config node (this shared object has no parameters or user options and need only be created once & re-used by every node instance (server side only))

Hope that makes sense.

Hey @Steve-Mcl,
Is this perhaps of any help to you?
Bart

A node's js is kind of in 3 sections.

You have the "global" part, the module.exports part and the RED.nodes.registerType section. I think of these as 3 layers. The global and the module.exports layers are common to all instances. So if you hold a variable there - as long as you don't update it from within the registerType function, it should - I believe - be common to all instances.

This is due to the way that Node.js's require function works. When you require a module, it is only ever loaded once even if it is later "required" again.

Hopefully someone will correct me if I have that wrong but that's how I think it works. I am not, after all, a professional Node developer.

If you look at the code in the v2 branch of uibuilder, you will see that I've moved a number of bits of processing out into this layer so that it is only processed once. Including the provision of the API's that I create.

1 Like