Home > Enterprise >  Use of this.function inside module.export
Use of this.function inside module.export

Time:10-01

How can I use functions and target them inside module.exports in Node.JS ? Says the function is not defined.

module.exports = (args) => {    

     this.myFunction = function(int){
          return int  ;
     };

     let test = this.myFunction(5);
};

// this.myFunction is not a function

CodePudding user response:

It is not very clear what you were trying to achieve but based on the code you have you could just give function a name

module.exports = (args) => {    

     function myFunction(int) {
          return int  ;
     };

     let test = myFunction(5);
};

CodePudding user response:

You can return the internal function from your exported function right.

module.exports = (args) => {    

     let myFunction = function(int){
          return int  ;
     };
     let test = myFunction(5);
     return myFunction;

};
const  moduleName   = require('./moduleName');
let newFunciton = moduleName(5);

You have the option to pass arguments too.

  • Related