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
  • 45,728
  • 6
  • 46
  • 64
mohamed nageh
  • 593
  • 4
  • 9
  • 26

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
  • 27,616
  • 4
  • 25
  • 48
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();