3

Hi I ran into a problem in Nodejs(with express) while trying to export a module with two functions which is structured as follows

exports.class1 = function(){
    return = {
         method1 : function(arg1, arg2){
             ...........
         },
         method2 : function (arg2, arg3, arg4){
             ..........  
         }

   };

 }

this module is saved in as module1.js when it is imported and used as follows an error occurs

var module1 = require('./module1');

module1.class1.method1(arg1, arg2);
Shenal Silva
  • 1,955
  • 5
  • 26
  • 37

1 Answers1

10

Your class1 should have code like bellow

exports.class1 = function(){
    return {
         method1 : function(arg1, arg2){
             console.log(arg1,arg2)
         },
         method2 : function (arg2, arg3, arg4){
             console.log(arg2,arg3,arg4);
         }

   };

 }

And you have to call it like bellow

module1.class1().method1(arg1, arg2);//because class1 is a function

A better way to do it to export an object

  exports.class1 = {
         method1 : function(arg1, arg2){
             console.log(arg1,arg2)
         },
         method2 : function (arg2, arg3, arg4){
             console.log(arg2,arg3,arg4);
         }
 }

And you can call it like

module1.class1.method1(arg1, arg2); //because here class1 is an object
Mritunjay
  • 25,338
  • 7
  • 55
  • 68