Hi Christian,
I have always assumed that this is not possible, but perhaps I'm totally wrong.
Thought the only way this is possible, is to "workaround" it like this:
Create a mylibrary.js file with your shared functions
exports.myfunction = function(aparameter) {
...
}
Use that js file in your backend js file:
module.exports = function(RED) {
// For example when the library file is in the same folder as the backend js file
const utils = require('./mylibrary');
function MyCustomNode(config) {
RED.nodes.createNode(this, config);
...
mylibrary.myfunction(someArgument);
}
}
And if you want to have the same function on the client, you need to add a script tag in your html file (to download it from your Node-RED server):
But this means your backend code should provide that file to your client:
RED.httpAdmin.get('/my_custom_node/*', function(req, res){
if (req.params[0].startsWith("anOptionalDirectoryName/")) {
var options = {
root: __dirname,
dotfiles: 'deny'
};
// Send the requested js library file to the client
res.sendFile(req.params[0], options);
}
}
So not perhaps an correct answer to your question. Because this way you not simply include the library in both client and server files. Instead the server makes the library file available to the client, so at least it allows you to reuse the same library functions both on client and server.
Nothing fancy really – it's a currency formatting function, and I want to make dead sure that currencies are formatted the same way on the server as in the client's status field.
Thanks Bart for the suggestion and taking your time. Your solution introduces an amount of boilerplate for a simple one-line function, but makes a lot of sense in the case of sharing libraries.
Nevertheless you made me realise I can call the formatting function on the server side and do a: