# Load external modules in the function node

**URL:** <https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451>\
**Category:** General\
**Tags:** function-node\
**Created:** [29 October 2023 19:18 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451 "2023-10-29T19:18:39Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henjoe/32/52335_2.png) [@Henjoe](https://discourse.nodered.org/u/Henjoe)\
**Post date:** [29 October 2023 19:18 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/1 "2023-10-29T19:18:39Z")

</div>

Hi, How can I load or use external libraries to a function node?  
I wanna use the "influx" node to my function. I read a lot of tutorials about it seems like it never worked to me. Hope you guys can help me.

This is my settings.js config:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/c/cc373637fcf5a38d882c98911af52636b6c9bc08.png)

And as per documentation, you can use this in function node via:

```auto
var INFLUX = global.get("influx")
var influxDB = new INFLUX.influxDB(influx_config)
influxDB.writePoints(msg.payload.data)
return msg;

```

But I have error:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a557a5032812b33d85b59822254c7282bd1235ee.png)

Am I missing something?

Thanks and Regards,

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 October 2023 17:39 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/2 "2023-10-30T17:39:06Z")

</div>

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/c/cc373637fcf5a38d882c98911af52636b6c9bc08.png) ](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/c/cc373637fcf5a38d882c98911af52636b6c9bc08.png)

That would be a correct approach (at least one way)  
Therefore, the error you are getting, might be more about how the use of the module/the module it's self

maybe its 🤷‍♂️

```auto
const INFLUX = global.get("influx")
const influxDB = new INFLUX(influx_config)
influxDB.writePoints(msg.payload.data)
return msg;

```

Given the error, it would suggest you are trying to instantiate something that isn't a class - I don't use influx, so don't know its module (or exports)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 October 2023 18:44 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/3 "2023-10-30T18:44:14Z")

</div>

Just checking that you know there is a widely used contrib node for accessing influxdb.

---

<div class="post-metadata">

**Author:** ![Henjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henjoe/32/52335_2.png) [@Henjoe](https://discourse.nodered.org/u/Henjoe)\
**Post date:** [30 October 2023 18:47 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/4 "2023-10-30T18:47:24Z")

</div>

Hi,  
I think it's working now I just need to restar the nodered.

Thanks!

---

<div class="post-metadata">

**Author:** ![Henjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henjoe/32/52335_2.png) [@Henjoe](https://discourse.nodered.org/u/Henjoe)\
**Post date:** [31 October 2023 02:48 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/5 "2023-10-31T02:48:43Z")

</div>

Hi Collin,  
Thanks for checking in, yes I know that, I actually using it for quite some time. How ever you cannot pass dynamically the name of the database in msg.db, you have to configure it on the influx node, in my case I need to pass it dynamically so I need to have customized function for it

Regards

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 October 2023 10:17 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/6 "2023-10-31T10:17:07Z")

</div>

Do you actually need to change it at run time, or would specifying the db via environment variable or in a subflow be good enough?

---

<div class="post-metadata">

**Author:** ![Henjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henjoe/32/52335_2.png) [@Henjoe](https://discourse.nodered.org/u/Henjoe)\
**Post date:** [31 October 2023 10:40 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/7 "2023-10-31T10:40:09Z")

</div>

I need to specify the DB outside the nodered platform. I don't want the user to open my nodered editor, so I need to create my own UI for this, and pass this parameter in nodered via runtime.  
I actually tried the subflow environment and pass it to influx node config, but seems like there is a problem. I don't have choice but to do like this.

Cheers,

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 October 2023 10:47 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/8 "2023-10-31T10:47:10Z")

</div>

> [@Henjoe](#):
>
> I need to specify the DB outside the nodered platform.

In that case you could use an environment variable.

---

<div class="post-metadata">

**Author:** ![Henjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henjoe/32/52335_2.png) [@Henjoe](https://discourse.nodered.org/u/Henjoe)\
**Post date:** [31 October 2023 13:11 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/9 "2023-10-31T13:11:00Z")

</div>

I actually tried that.

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

and set the influx node to be like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b0e5b6d06a3c794b423c52f3b786aea4d6e7c98b.png)

and set environment:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d4209e71f5dd7acbcb30ad3e38f5e22b06f98e33.png)

but gives me the ff error when using the subflow:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/4/b41f8df2fda5700068fb356111b1095c84d2a6d7.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 October 2023 14:24 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/10 "2023-10-31T14:24:50Z")

</div>

I didn't mean in a subflow. You said you wanted to setup the db outside node-red, so set an environment variable and use it node-red.

---

<div class="post-metadata">

**Author:** ![Henjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henjoe/32/52335_2.png) [@Henjoe](https://discourse.nodered.org/u/Henjoe)\
**Post date:** [31 October 2023 16:20 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/11 "2023-10-31T16:20:57Z")

</div>

How can I use that environment variable to input it in influx node?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 October 2023 16:30 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/12 "2023-10-31T16:30:03Z")

</div>

The same was as you did in a subflow that did not work, but not in a subflow. I imagine the reason it did not work was because it was in a subflow, are you using the latest version of node-red?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 October 2023 16:57 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/13 "2023-10-31T16:57:40Z")

</div>

I suspect that the reason you had trouble using the node in a subflow may be down to this.

> [@Using Influxdb node in a subflow - cannot set scope of config node](https://discourse.nodered.org/t/using-influxdb-node-in-a-subflow-cannot-set-scope-of-config-node/82499):
>
> If I add an MQTT node to a subflow, then in the server setup I select to add a new server, then at the bottom of config node's edit screen I get a dropdown that allows me to select the subflow as the context for the node. However, if I do the same with Influxdb node from node-red-contrib-influxdb then the db config node does not have that dropdown. That, I believe, prevents me from specifying the database via subflow properties, which requires a separate config node for each instance of the su…

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 December 2023 16:58 UTC](https://discourse.nodered.org/t/load-external-modules-in-the-function-node/82451/14 "2023-12-30T16:58:19Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
