I'm having a problem accessing a field in an object that is required/imported at the top of a file, the field is accessible however when importing the object again at a later point in the code.
I've made a little example to show what I mean: [Execute code on CodingGround]
main.js:
var ClassA = require('./ClassA');
ClassA.init();
ClassA.js:
var ClassB = require('./ClassB');
var ClassA = function() {};
ClassA.init = function() {
    // Define a variable in ClassA
    this.myVar = 'My Value';
    // Create an instance of ClassB, and try to print the defined variable
    var myClass = new ClassB();
    myClass.printMyVar();
};
module.exports = ClassA;
ClassB.js:
var ClassA = require('./ClassA');
var ClassB = function() {};
ClassB.prototype.printMyVar = function() {
    // Print the variable (not working)
    console.log('My Var: ' + ClassA.myVar);
    // Require ClassA again
    ClassA = require('./ClassA');
    // Print the variable again (working)
    console.log('My Var (again): ' + ClassA.myVar);
};
module.exports = ClassB;
I get the following output when main.js is executed:
My Var: undefined
My Var (again): My Value
Why does the first line show the variable is undefined, and why does it show the variable properly after requiring/importing the object again?
Is this caused by a circular dependency and if so, how can I avoid this problem in the future?
 
    