Your problem seems tricky because you have a circular dependency: main.js
depends on the functionality of my_class
and my_class
depends on the data of main.js
.
By putting the data of main.js
into the global
object you resolve the circular dependency:
main.js
depends on the functionality ofmy_class.js
main.js
depends on the data in theglobal
objectmy_class.js
depends on the data in theglobal
object
Now, to get rid of putting the data into the global
object, implement a third module in let us say data.js
. Then you require the sources like this:
main.js
requiresdata.js
main.js
requiresmy_class.js
my_class.js
requiresdata.js
Since modules in node.js are singletons both main.js
and my_class.js
will get the same instance of data.js
.