Eric
Eric

Reputation: 10626

Use of this.function inside module.export

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

Upvotes: 0

Views: 55

Answers (2)

Tushar Shahi
Tushar Shahi

Reputation: 20441

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.

Upvotes: 1

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

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);
};

Upvotes: 1

Related Questions