-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
module.children behavior does not match documentation (only tracks first requires) #9371
Comments
Note: I did not (perhaps rudely) fill out the template with version/platform information, since this affects multiple versions and is probably a documentation bug. Testing this is trivial, however here is a simple example showing the issue. |
This is definitely a good point. I am even more concerned about the fact that modules are not cached if they throw an try {
require('./b.js');
} catch(err) {
try {
require('./b.js');
} catch(err) {
}
}
console.log(`There are ${module.children.length} child modules`);
for(let child of module.children) {
if(!require.cache[child.id])
console.log(`${child.id} is not in require.cache!`);
} This prints something like
Is this intended behavior? (See also #13386 for a similar topic.) Which solution would we prefer?
Edit: @bmeck Just made me realize that adding a module as the child of multiple other modules would be inconsistent as well as each module only has one parent module. Or, to put it with his words, "i would say nothing is consistent about module.parent or module.children". |
@tniessen the more useful approach to me is to deprecate It's not that much work to fix this, I think I even had a patch kicking around at some point. It's useful for introspection/reflection type behaviours. |
@nodejs/modules Anything happening with this? Is this still an issue? Is this something that might benefit from a |
@Trott the modules team is tasked with ESM integration into Node.js - I think this is something that is out of scope for us - if this is stalled then I think |
@benjamingr Sorry about that! I assumed nodejs/modules was the module subsystem and there was a nodejs/es-modules team or something but it looks like I was wrong. |
I think this is definitely in scope for the modules group, because anything supported in CJS has to be explicitly supported, or not, in ESM. In this case, runtime reflection on the module graph is not a feature I think node should support in either CJS or ESM, so I'd be strongly in favor of deprecating it in CJS. |
@ljharb I agree in general though the implementation in this case (module.children) doesn't really have much to do with how esm/cjs interacts in particular. |
Couldn't we just make a change in the docs? The behavior seems pretty normal, feel free to change it if you want, but I believe we should just tag this as a "good first issue" with a single doc change. |
Taking @ryzokuken's advice: if anyone wants to tackle this as a first issue and fix the docs - please let us know if you want/need help :) |
Updates the module.children description in the docs to match current functionality in that it will only contain the modules required for the first time by this module. Fixes: nodejs#9371
Updates the module.children description in the docs to match current functionality in that it will only contain the modules required for the first time by this module. Fixes: #9371 PR-URL: #21672 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: James M Snell <jasnell@gmail.com>
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. Sincemodule.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
andmodule.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 ofmodule
fieldsrequirers
andrequirees
as respective analogues ofparent
andchildren
, 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'schildren
structure (which only works the first time it is required, since after that the module is initialised and cached), we run after everyrequire
, pushing this module into the child'srequirers
. 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:
module.children
to match the documentation (fix basically described above)Possibly related to #7131 and #3933
The text was updated successfully, but these errors were encountered: