This file imports/requires npm packages, which I have listed in dependencies and I know they exist.
When I try loading this up in Node-RED it give's me error that it cannot find the module.
It also gives some errors about having /, ./ or ../ in the path, but that should not be necessary because it is a global module. If I include ./ws it still throws error.
Any idea on why it is behaving like this? And how shall it be done
OK, the normal way to do this is for your node to include a REST API. You can then call that using jQueries helpers from the front end. There is a simple example in the code of the core serial node. More extensive examples are available in the uibuilder node.
Let me clarify, I am trying to populate a dropdown in node's properties, and I am doing it in onEditPrepare. The data that I want to get has to be parsed from the web, because it changes dynamicaly. So I need to make an update each time when I do onEditPrepare, thus websocket or http call.
you mean the config dialog in the Node-RED editor?
This is how I do it (don't know if it's right but works for me)
in your yourNode.js inside your main function you establish a listener for http requests
RED.httpAdmin.get("/yourNode/"+node.id, RED.auth.needsPermission('your-node.read'), function(req,res) {
console.log(`http request "${req.query.query}" from ip: ${req.headers.host}`);
var returnItems = []; // list of Items to send back to the frontend
var config = req.query;
switch (config.query) {
case 'sendMe':
returnItems = ['take this'];
break;
default:
returnItems = `Error! query "${config.query}" unknown`;
break;
}
res.json(returnItems);
})
and in your yourNode.html (where it suites you) you call your listener and get the data in the done callback
This is the method used by the Serial node that Julian linked to previously.
But a very important bit is to not start the path with a / in the html side. That means it will work even if httpAdminRoot has been set to move the editor to a different root URL.