# Issue using a class definition from external module in function node

**URL:** <https://discourse.nodered.org/t/issue-using-a-class-definition-from-external-module-in-function-node/56225>\
**Category:** General\
**Created:** [6 January 2022 21:52 UTC](https://discourse.nodered.org/t/issue-using-a-class-definition-from-external-module-in-function-node/56225 "2022-01-06T21:52:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![grahamwo](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@grahamwo](https://discourse.nodered.org/u/grahamwo)\
**Post date:** [6 January 2022 21:52 UTC](https://discourse.nodered.org/t/issue-using-a-class-definition-from-external-module-in-function-node/56225/1 "2022-01-06T21:52:49Z")

</div>

Hi all. I'm somewhat new to node.js and Node-Red, so please bear with me as I may be overlooking something obvious. I have some class definitions in a private npm module that I created. In my index.js file I use the following to define a class and export it.

```auto
class Phase {
//class definition here
};

export default Phase;

```

Then I published the module and installed it globally. I updated the settings.js file to require the module I created based on this documentation: [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules)

After that, I restart node-red and add the module into the function node under the setup tab. Then, I try to use the class definition as follows in my function node:

```auto
var class = global.get('helper); //helper is the name used in settings.js file
let test = new class.Phase();

```

When I run my flow I get the error:

```auto
"TypeError: class.Phase is not a constructor"

```

Any help resolving this would be greatly appreciated as I'm at a loss for how to resolve this issue. I'm assuming this is an issue with how I export the class definition. I've also tried exporting it using

```auto
module.exports = Phase;
module.exports.Phase = Phase;
exports.Phase = Phase;

```

Thank you!

---

<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:** [6 January 2022 23:27 UTC](https://discourse.nodered.org/t/issue-using-a-class-definition-from-external-module-in-function-node/56225/2 "2022-01-06T23:27:01Z")

</div>

Hi ..

my javascript never got that advanced in order to use classes  
but since nobody else replied i'll have a go 😉

> [@grahamwo](#):
>
> `var class = global.get('helper);`

isn't `class` a reserved keyword ? have you tried naming your `var` something else ?

have you also tried in your js module

```auto
export class Phase {
//class definition here
};

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 January 2022 00:20 UTC](https://discourse.nodered.org/t/issue-using-a-class-definition-from-external-module-in-function-node/56225/3 "2022-01-07T00:20:11Z")

</div>

Have a look at my [Drayton Wiser node.js module](https://github.com/TotallyInformation/node-drayton-wiser). It does something similar to what you want. Hopefully it should give you some hints.

---

<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:** [7 January 2022 00:34 UTC](https://discourse.nodered.org/t/issue-using-a-class-definition-from-external-module-in-function-node/56225/4 "2022-01-07T00:34:05Z")

</div>

Hi @grahamwo

If you have the following in your module's index.js:

```auto
class Phase {
//class definition here
};

module.exports = Phase

```

And added it to `functionGlobalContext` with:

```auto
functionGlobalContext: {
    helper:require('your-custom-module')
}

```

Then you should be able to use it in the Function node as:

```auto
const Phase = global.get("helper");
let test = new Phase();

```

* * *

As you'll likely want to export multiple things from your module, you could do:

```auto
module.exports = {
   Phase
}

```

and then the Function node would be:

```auto
const helper = global.get("helper");
let test = new helper.Phase();

```

---

<div class="post-metadata">

**Author:** ![grahamwo](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@grahamwo](https://discourse.nodered.org/u/grahamwo)\
**Post date:** [7 January 2022 15:50 UTC](https://discourse.nodered.org/t/issue-using-a-class-definition-from-external-module-in-function-node/56225/5 "2022-01-07T15:50:34Z")

</div>

I do have multiple classes defined that I wanted to put in the module. I tried using

```auto
module.exports = {
   Phase
}

```

This still gave me the same error as before. I then tried just using

```auto
module.exports = Phase

```

This fixes the error about not being a constructor. However, now when I try to use my classes member functions they always return undefined. For reference, I previously had this class definition in a function node, so I know the class is working. I also took a look at @TotallyInformation repository and seem to be doing the same as him, other than using class instead of function. I made sure that I published the module and installed again globally after making changes. I also make sure that I restart node-red after making changes to the module. Any ideas as to why I can't call my member functions?

For example:

```auto
const Phase = global.get("helper");
let test = new Phase();
return test.numParams; //returns undefined, numParams is a getter in this class

```

---

<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:** [8 March 2022 15:50 UTC](https://discourse.nodered.org/t/issue-using-a-class-definition-from-external-module-in-function-node/56225/6 "2022-03-08T15:50:42Z")

</div>

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