# Using other node.js modules

**URL:** https://discourse.nodered.org/t/using-other-node-js-modules/364
**Category:** FAQs
**Created:** [24 May 2018 17:27 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364 "2018-05-24T17:27:02Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)
#### Post date: [24 May 2018 17:27 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/1 "2018-05-24T17:27:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [24 May 2018 17:29 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/2 "2018-05-24T17:29:20Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)
#### Post date: [24 May 2018 17:36 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/3 "2018-05-24T17:36:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [24 May 2018 18:09 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/4 "2018-05-24T18:09:49Z")

</div>

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

---

<div class="post-metadata">

### Author: ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)
#### Post date: [24 May 2018 18:11 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/5 "2018-05-24T18:11:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [24 May 2018 20:43 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/6 "2018-05-24T20:43:52Z")

</div>

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](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.

---

<div class="post-metadata">

### Author: ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)
#### Post date: [24 May 2018 21:20 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/7 "2018-05-24T21:20:14Z")

</div>

Thanks Knolleary,

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

Thanks for your reply anyway.

Regards

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [24 May 2018 22:23 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/8 "2018-05-24T22:23:17Z")

</div>

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_.

---

<div class="post-metadata">

### Author: ![JorgeAlberto91MS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorgealberto91ms/32/36444_2.png) [@JorgeAlberto91MS](https://discourse.nodered.org/u/JorgeAlberto91MS)
#### Post date: [15 July 2020 17:48 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/9 "2020-07-15T17:48:03Z")

</div>

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](https://www.npmjs.com/package/i2c-mcp4725/v/1.0.4).

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [15 July 2020 17:59 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/10 "2020-07-15T17:59:21Z")

</div>

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](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules)

---

<div class="post-metadata">

### Author: ![JorgeAlberto91MS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorgealberto91ms/32/36444_2.png) [@JorgeAlberto91MS](https://discourse.nodered.org/u/JorgeAlberto91MS)
#### Post date: [16 July 2020 17:01 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/11 "2020-07-16T17:01:27Z")

</div>

OK, thanks...

---

<div class="post-metadata">

### Author: ![JorgeAlberto91MS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorgealberto91ms/32/36444_2.png) [@JorgeAlberto91MS](https://discourse.nodered.org/u/JorgeAlberto91MS)
#### Post date: [5 August 2021 19:30 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/12 "2021-08-05T19:30:22Z")

</div>

Is it possible to use constructors when importing external modules?  
If it is possible, how can it be done in Node-Red 2.0?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [5 August 2021 21:14 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/13 "2021-08-05T21:14:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![JorgeAlberto91MS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorgealberto91ms/32/36444_2.png) [@JorgeAlberto91MS](https://discourse.nodered.org/u/JorgeAlberto91MS)
#### Post date: [16 August 2021 19:30 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/14 "2021-08-16T19:30:41Z")

</div>

```auto
//-----------------------------------------------------------------------------------------------------------
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](https://www.npmjs.com/package/ethernet-ip)

How could I declare the constructor in order to avoid the syntax error

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [16 August 2021 19:59 UTC](https://discourse.nodered.org/t/using-other-node-js-modules/364/15 "2021-08-16T19:59:26Z")

</div>

With the newer versions of Node-red you can enable the `functionExternalModules` option  
You can read more on how to set that up [here](https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option)

**In function node Setup tab :**

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4af980d5c4a0040845f94f351ed151d312498a81.png)

**In On Message Tab :**

```auto
const Controller = ethernetIp.Controller;

const PLC = new Controller();

//console.log(PLC)

```
