0
// module1
const B = require("./module2")

module.exports = class A {
   constructor() {}

   static fun(){
      B.func().....
   }
}
// module2
const A = require("./module1")

module.exports = class B {
   constructor(){}

   static fun(){
      A.fun()...
   }
}

When i console typeof class 'B' from A instead of showing function it showing type as an object ie:-the class becoming an empty object {} how to access class methods correctly. It showing an error like

A.fun((...) => {
              ^

TypeError: Cannot read property 'fun' of undefined
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Afsal Kp
  • 13
  • 2
  • 1
    This looks like a cyclic dependency. A wants something form B and B wants something from A. Not sure if this will work. – Tushar Shahi May 25 '21 at 10:09
  • 1
    @TusharShahi [Cyclic dependencies can work](https://nodejs.org/api/modules.html#modules_cycles), but you are right in that in this case, it comes with caveats. – RickN May 31 '21 at 12:40

1 Answers1

1

it seems that the two modules point to each other try this:

// A.js
import B from './B.js'
export default class A{}

// B.js
import A from './A.js'
export default class B extends A{}
Barrios
  • 55
  • 5