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
  • 31,322
  • 6
  • 25
  • 44
mohamed nageh
  • 171
  • 2
  • 15

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
  • 16,831
  • 3
  • 12
  • 27
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();
jajabailio
  • 11
  • 1