# Using module in function node

**URL:** https://discourse.nodered.org/t/using-module-in-function-node/33090
**Category:** General
**Created:** [19 September 2020 17:53 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090 "2020-09-19T17:53:16Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mattfusf](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mattfusf](https://discourse.nodered.org/u/mattfusf)
#### Post date: [19 September 2020 17:53 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/1 "2020-09-19T17:53:16Z")

</div>

I am trying to use the [themeparks API package](https://github.com/cubehouse/themeparks) in Node-Red. I've got it installed on my machine, and have added it to the settings.js file, but my JavaScript knowledge is a bit limited on how to create an object that persists throughout the flow. I've tried creating a global variable in the setup tab of the function node, but nothing happens after I deploy and run the flow. Any suggestions on how to make this work are appreciated.

Setup code

```auto
Themeparks = global.get('themeparksAPI');
DisneyWorldMagicKingdom = new Themeparks.Parks.WaltDisneyWorldMagicKingdom();
global.set('wdwmk',DisneyWorldMagicKingdom);

```

Function code

```auto
wdwmk=global.get('wdwmk');
msg.openingtimes=wdwmk.GetOpeningTimes();
msg.waittimes=wdwmk.GetWaitTimes();
return msg;

```

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 September 2020 18:08 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/2 "2020-09-19T18:08:29Z")

</div>

I would first verify that the npm module has been correctly installed and added to settings.js (no typos etc.). Try putting adding a function node with only:

```auto
node.warn(global.get('themeparksAPI'));

```

... in the middle tab of the function node and see what it prints in the debug tab.

---

<div class="post-metadata">

### Author: ![mattfusf](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mattfusf](https://discourse.nodered.org/u/mattfusf)
#### Post date: [19 September 2020 18:19 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/3 "2020-09-19T18:19:14Z")

</div>

Thanks, I gave that a try and it appears to be installed based on the debug output

 ![Screen Shot 2020-09-19 at 2.15.22 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b59a1c9026803cd044c1e3a3226a180bc0147915.png)

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 September 2020 18:32 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/4 "2020-09-19T18:32:26Z")

</div>

The node's documentation has examples. It seems `GetOpeningTimes()` ans `GetWaitTimes()` return a `Promise`, meaning they all called asynchronously. If you are running a current Node-RED version (v1.1.x), wee if it makes a difference if you change your function code to:

```auto
const wdwmk = global.get('wdwmk');
msg.openingtimes = await wdwmk.GetOpeningTimes();
msg.waittimes = await wdwmk.GetWaitTimes();
return msg;

```

Edit:  
It would likely be better to store `wdwmk` in the node specific context. So:

```auto
// setup
context.set('wdwmk',DisneyWorldMagicKingdom);

// function
const wdwmk = context.get('wdwmk');

```

---

<div class="post-metadata">

### Author: ![mattfusf](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mattfusf](https://discourse.nodered.org/u/mattfusf)
#### Post date: [19 September 2020 18:56 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/5 "2020-09-19T18:56:44Z")

</div>

Thanks for the async hint, I'm not too familiar with that.

It runs now but never seems to continue beyond the function node (I just have a debug node after this to dump the complete msg object)

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 September 2020 19:25 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/6 "2020-09-19T19:25:05Z")

</div>

Do you get any other debug output if you wrap the calls in a try catch block?

```auto
const wdwmk = global.get('wdwmk');
try {
  msg.openingtimes = await wdwmk.GetOpeningTimes();
  msg.waittimes = await wdwmk.GetWaitTimes();
  return msg; // also try: node.send(msg);
} catch (error) {
  node.error(error)
}

```

---

<div class="post-metadata">

### Author: ![mattfusf](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mattfusf](https://discourse.nodered.org/u/mattfusf)
#### Post date: [19 September 2020 19:42 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/7 "2020-09-19T19:42:47Z")

</div>

Thanks; I think I'm getting somewhere now. This works, however, when ever I deploy the flow a second time, I get the following:

```auto
Error: 
 Failed to create second instance of "WaltDisneyWorldMagicKingdom" object.
 Please only create one instance of each location and re-use it.

```

Is there something in the close code I can add to destroy the instance I've created?

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 September 2020 20:04 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/8 "2020-09-19T20:04:23Z")

</div>

Try adding to the close part:

```auto
global.set('wdwmk', null); // or context.set if you used it

```

This should remove the last reference to the object so that the garbage collection can delete the object.

---

<div class="post-metadata">

### Author: ![mattfusf](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mattfusf](https://discourse.nodered.org/u/mattfusf)
#### Post date: [19 September 2020 20:09 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/9 "2020-09-19T20:09:03Z")

</div>

Thanks, gave that a shot but still getting it on second deploy (even after server restart)

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [19 September 2020 20:14 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/10 "2020-09-19T20:14:59Z")

</div>

Rather than do it on close. Check for existence at start and only set it if it doesn’t already exist in global.

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 September 2020 20:15 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/11 "2020-09-19T20:15:54Z")

</div>

Quite frustrating. It seems like the library tries to be too smart. Let me think for a moment.

Edit: Try what @dceejay suggested!

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 September 2020 20:19 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/12 "2020-09-19T20:19:00Z")

</div>

Could using node context instead of global also help in this case (as I suggested earlier on)?

---

<div class="post-metadata">

### Author: ![mattfusf](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mattfusf](https://discourse.nodered.org/u/mattfusf)
#### Post date: [19 September 2020 20:22 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/13 "2020-09-19T20:22:18Z")

</div>

Thanks; I tried this, and while it only loads the first time, on subsequent deploys, the function can't seem to see it any longer.

```auto
TypeError: Cannot read property 'GetWaitTimes' of null

```

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 September 2020 21:21 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/14 "2020-09-19T21:21:49Z")

</div>

Did you remove the code from the close tab first?

---

<div class="post-metadata">

### Author: ![mattfusf](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mattfusf](https://discourse.nodered.org/u/mattfusf)
#### Post date: [19 September 2020 21:33 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/15 "2020-09-19T21:33:40Z")

</div>

🤦‍♂️ ha no. That did the trick! Thanks so much for your help with this, I'm able to get data back and it seems to work through a re-deploy. Thanks again to you and @dceejay !

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 September 2020 21:35 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/16 "2020-09-19T21:35:34Z")

</div>

Great! I'd be curious to hear what you're building based on a theme park API?

---

<div class="post-metadata">

### Author: ![mattfusf](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mattfusf](https://discourse.nodered.org/u/mattfusf)
#### Post date: [19 September 2020 21:39 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/17 "2020-09-19T21:39:59Z")

</div>

I had a bunch of iOS shortcuts written that directly accessed the Disney API so I could get wait times and such via Siri. They deprecated that version of the API, and the new one is a hassle to work with (especially given the limitations of shortcuts) so I am going to use this as a backend instead and let the package maintainers keep up with the Disney API changes.

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [19 September 2020 21:45 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/18 "2020-09-19T21:45:39Z")

</div>

Ah so you (and I assume your family) are frequent visitors or maybe you do some work there. Oh well, it's really not my business. I was just somewhat surprised to find there are API's for that and even a Node module. It's not relevant to me as I don't live in the US but just sounds interesting.

---

<div class="post-metadata">

### Author: ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)
#### Post date: [19 September 2020 21:49 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/19 "2020-09-19T21:49:24Z")

</div>

> [@ristomatti](#):
>
> It's not relevant to me as I don't live in the US

this node module also has the Europa Park in Germany 🙂 or Stockholm or similar  
I am also curious why there is a node module for this 🙂

---

<div class="post-metadata">

### Author: ![mattfusf](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mattfusf](https://discourse.nodered.org/u/mattfusf)
#### Post date: [19 September 2020 21:49 UTC](https://discourse.nodered.org/t/using-module-in-function-node/33090/20 "2020-09-19T21:49:39Z")

</div>

Frequent visitors, yes! 🙂 The node module is very much unofficial, I believe most of it came from reverse engineering the websites and mobile apps. It supports theme parks around the world, not just Disney World.

[Next page](https://discourse.nodered.org/t/using-module-in-function-node/33090.md?page=2)
