As this is the place to put some dreams…
I know there is possible to use other node.js modules however and after one year playing with node red is for me still not clear how to use this.
Will be great if there is some more easy way to implement this integration or at least some tutorial that makes easier for people like me to understand and make use of it.
In my opinion this will speed up much more the use of node red for many more ppl.
Regards
Hi - I’ve moved this to the #development:feature-requests category.
Can you expand a bit more what you mean here. I’m not clear on what you’re suggesting. You mention ‘this integration’ - what integration are you referring to?
Hello Knolleary
Thanks for your reply.
Is a global idea not speaking about a specific one.
Just as example time ago I was trying to understand how to use node.js modules already created to use some I2C devices or to use a Ultrasonic Sensor HC-SR04, today I have seen a partner checking to use one developed to use a gopro, I think there will be plenty of examples, I also know this is possible to be handled but for me is not clear at all how to manage this and at the end simply I scrapped the idea.
I guess that probably having some easy way to manage this or maybe some clear tutorial will bring light on this topic can push a lot of people to get interested into testing or using node-red.
Is just a personal feedback, maybe helps, maybe is a bit silly … I don’t know.
Regards
Just to be clear then - your question can be boiled down to how to use arbitrary node.js modules within node-red?
Yes, exactly, for me this is not clear and having this on my hands I know I can make a huge increase on the capabilities, is clear that maybe not all will work but at least to know how to try it.
Regards
Reproducing my reply on the other thread:
The main route to using any node module in Node-RED is for someone to wrap the module in a node-red node. That requires some time and effort to do right.
It is also possible to load additional node modules and access them from within a Function node. This is described, slightly obliquely here: https://nodered.org/docs/writing-functions#global-context
You do it by loading the module in your settings.js
file, and adding it to the functionGlobalContext
setting. The Function node can then access the module via global context. The docs have an example of doing that with the built-in os
module.
Thanks Knolleary,
I had checked already this info and for me was not totally how to.
Thanks for your reply anyway.
Regards
One other “generic” way to use an npm module is through the node-red-contrib-npm
node – you configure it to pull in a specific library, then call write a bit of code to call its functions.
Here is an example of how I am using the suncalc
module to get today’s sunrise/sunset timestamps. Select Module Style: "Custom"
and enter this code into the Module Invocation editor:
var dts = +msg.timestamp || Date.now();
var lat = +msg.latitude || 35.75;
var lon = +msg.longitude || -80.80;
var dtm = new Date(dts);
// NPM module exposed as variable, npm_module
var times = npm_module.getTimes(dtm, lat, lon);
var topic = msg.topic || "*";
if (topic === "*") {
return times;
}
else if (times.hasOwnProperty(topic)) {
return times[topic];
}
else {
return {};
}
It’s not a super efficient way to use this module, since it has to be loaded from the internet every time node-red starts, but it was pretty simple to set up. YMMV - Just be aware that not every module will work with this method.
I read that it's possible to load globally installed nodes, is that true? ( npm install -g <package_name> )
If so, how could I use them within the function node?
I want to use this particular node within the function node: https://www.npmjs.com/package/i2c-mcp4725/v/1.0.4.
Hi @JorgeAlberto91MS - my comment that you have replied to still applies to global modules. You load them in via functionGlobalContext
in your settings file - https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules
Is it possible to use constructors when importing external modules?
If it is possible, how can it be done in Node-Red 2.0?
Do you have an example of what you mean?
The function node will pre-require the module to the variable name you pick. How you then use that will depend on the module.
//-----------------------------------------------------------------------------------------------------------
const { Controller } = require("ethernet-ip"); // This line is not necessary because Node-Red can now load external libraries from the node configuration.
//-----------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------
const PLC = new Controller(); // This line of code is showing error in Node-Red
//-----------------------------------------------------------------------------------------------------------
// Controller.connect(IP_ADDR[, SLOT])
// NOTE: SLOT = 0 (default) - 0 if CompactLogix
PLC.connect("192.168.1.1", 0).then(() => {
console.log(PLC.properties);
});
Source: ethernet-ip - npm
How could I declare the constructor in order to avoid the syntax error
With the newer versions of Node-red you can enable the functionExternalModules
option
You can read more on how to set that up here
In function node Setup tab :
In On Message Tab :
const Controller = ethernetIp.Controller;
const PLC = new Controller();
//console.log(PLC)