1

I have an module exported as the following, I want to call function in another is that possible in that case

module.exports = {
  first:() => {
    // this is my first function
  },
  second:() => {
    // I want to call my first function here
    // I have tried this 
    this.first()

  }
}
Nick Parsons
  • 38,409
  • 6
  • 31
  • 57
mohamed nageh
  • 439
  • 5
  • 23

2 Answers2

1

You can define module.exports at first in a variable and use it in second as follows:

First file (other.js):

const _this = (module.exports = {
  first: () => {
    console.log("first");
  },
  second: () => {
    _this.first();
  }
});

Second file (index.js):

import other from "./other";

other.second();

Output:

first
Majed Badawi
  • 25,448
  • 4
  • 17
  • 37
0

You can access it from the other module like this

const {first, second } = require('./module2');
first();
second();

or

const collectedFunctions = require('./module2');

collectedFunctions.first();
collectedFunctions.second();