Issue using a class definition from external module in function node

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.

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

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:

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:

"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

module.exports = Phase;
module.exports.Phase = Phase;
exports.Phase = Phase;

Thank you!

Hi ..

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

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

have you also tried in your js module

export class Phase {
//class definition here
};

Have a look at my Drayton Wiser node.js module. It does something similar to what you want. Hopefully it should give you some hints.

1 Like

Hi @grahamwo

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

class Phase {
//class definition here
};

module.exports = Phase

And added it to functionGlobalContext with:

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

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

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

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

module.exports = {
   Phase
}

and then the Function node would be:

const helper = global.get("helper");
let test = new helper.Phase();
2 Likes

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

module.exports = {
   Phase
}

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

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:

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

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