-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Unexpected behavior in module customization hooks / loader, for require
#55808
Comments
Maybe I should mention my use case here: I'm trying to use the module hooks/loader with SEA, to distribute my project, in 1+n files. This requires the hooks to resolve and load paths that do not really exist on the hard drive. |
@nodejs/loaders |
I feel that the approach taken in #47999 is just broken and we should just go back to using |
@joyeecheung I generally liked the idea of having a dedicated loader thread. I agree with you about reusing cjs loader code, but not Mixing the cjs and module loader is probably inevitable because cjs and es modules may load each other in user code. For this specific issue, it's just a bug-level thing, no need to overturn the whole approach. |
For context the overturn has already been discussed before: I even have to push back against deprecating or just removing module.register() now when I was adding module.registerHooks() because the off-thread approach have many bugs and edge cases that have not been fixed and there's generally a lack of interest in fixing them. But I was just not in favor in removing the async hooks immediately due to not wanting to disrupt existing usage, not that I think it's a path worth pursuing further (I for one, am not interested in fixing the off-thread code because it just doesn't fit well internally IMO, and provides no good story for CJS monkey patching migration. I would not oppose against others trying to fix it, but you can probably see a lack of interest in fixing it from the radio silence of this issue). |
It seems I am one of the early adopters of module hooks.
I liked the idea of the module customization hooks, and tried to use it in my project.
However, I believe the current behavior of
require
handling in the esm loader is buggy.Relevant source code, here
node/lib/internal/modules/esm/translators.js
Lines 130 to 159 in 69f8794
The resolving of the
specifier
is done in the main thread, usingModule._resolveFilename
, before being sent to the customization hooks.The resolver hook is then receiving a resolved
file://
URL, instead of the realspecifier
.These are not expected according to the documentation of the module customization hooks.
And for my use case, it has made the hooks useless.
To make it clear there are two bugs:
The hooks, therefore, cannot really work, because if the "expected" file does not really exist on the hard drive. The cjs
Module._resolveFilename
will throw an error in the first place.specifier
, if the first bug would not trigger.The root of this issue might be the default resolver in customization hooks does not support resolving CJS modules.
An easy fix would be to move the
Module._resolveFilename
into the default resolver.The loading of
.node
native add-ons may also get in the way.But IMO it should also go through the hooks, and eventually be loaded as
{type: 'commonjs', source: undefined, url: 'file://... .node'}
The text was updated successfully, but these errors were encountered: