Closed alexpi closed 5 years ago
You're not getting anything wrong—CommonJS circular dependencies are only filled in after the initial run of the module code. So if you have code like this instead....
const b = require("./b") // Note no curly braces
exports.test = function() { console.log(b.b) }
Calling test
later on would work. But when the console.log
in your code runs, the other module indeed hasn't initialized yet, so you can't use it. (And destructuring it like {b}
doesn't work because when that statement is evaluated, the object is still empty, so the b
you read is undefined
.)
Since this in not an issue with the book, feel free to ignore or point me to a relevant place (tried Stack Overflow, but got no answer) to ask my question.
I can't understand the hint on the last exercise of Chapter 10. http://eloquentjavascript.net/10_modules.html#i_E/zWqBFdy8
I made an example in order to understand:
I am trying to trace the order of operations on the above:
require.cache[name].exports
. This seems to return an empty object since the wrapper function hasn't arrived to the point of adding the exports value to the module.exports object.console.log(b)
and outputs the empty object.What am I getting wrong?