Description
TL;DR module.children
does not behave according to documentation.
What be the expected behaviour of module.children
?
Documentations states that module.children
is "the module objects required by this one." This is false, as it only lists the children that were required for the first time by the module. Since module.parent
is correctly documented as containing "the module that first required this one", it seems like this may be a documentation bug. If that's the case, we should correct the documentation.
Considering the issue on another level, what is the purpose of module.children
and module.parent
? Having a full graph of the first requires, or a partial/incomplete graph of all require relationships, does not seem at all useful to me. Why do these exist? I propose the addition of module
fields requirers
and requirees
as respective analogues of parent
and children
, but ones which will keep track of all requires. The code will be essentially the same as what is currently in place, except that instead of running on module initialisation and pushing this module into the parent module's children
structure (which only works the first time it is required, since after that the module is initialised and cached), we run after every require
, pushing this module into the child's requirers
. This will generally also only run once per uncached module (except for modules with conditional or lazy requires), except still works even if the children are cached.
So, fix options here are:
- consider this a documentation bug and fix the documentation
- change the behaviour of
module.children
to match the documentation (fix basically described above)