There is a good and clear example provided in the node.js documentation . It sheds more light on the object "partially executed" and cyclical dependencies.
When there are calls to circle require (), the module may not have completed execution when it is returned.
Consider this situation:
a.js:
console.log('a starting'); exports.done = false; const b = require('./b.js'); console.log('in a, b.done = %j', b.done); exports.done = true; console.log('a done');
b.js:
console.log('b starting'); exports.done = false; const a = require('./a.js'); console.log('in b, a.done = %j', a.done); exports.done = true; console.log('b done');
main.js:
console.log('main starting'); const a = require('./a.js'); const b = require('./b.js'); console.log('in main, a.done=%j, b.done=%j', a.done, b.done);
When main.js loads a.js, then a.js takes turns loading b.js. At this point, b.js is trying to download a.js. To prevent an infinite loop, an incomplete copy of the a.js export object is returned to the b.js module. Then b.js completes the download, and the export object is provided to the a.js. module.
By the time main.js loaded both modules, they were both finished. The result of this program will be:
node main.js
main starting a starting b starting in b, a.done = false b done in a, b.done = true a done in main, a.done=true, b.done=true
source share