Using other node.js modules

#1

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

0 Likes

#2

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?

0 Likes

#3

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

0 Likes

#4

Just to be clear then - your question can be boiled down to how to use arbitrary node.js modules within node-red?

0 Likes

#5

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

0 Likes

#6

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.

0 Likes

#7

Thanks Knolleary,

I had checked already this info and for me was not totally how to.

Thanks for your reply anyway.

Regards

0 Likes

#8

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.

0 Likes