-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
node --experimental-modules main
doesn't work (works only if extension supplied)
#16476
Comments
To explain the logic here - the main module is resolved to an absolute URL ( Absolute paths are not passed through the file extension checks (.mjs, .js, .node, .json, ...) in the default NodeJS ES module resolver implemented (although I believe they are in If this is an ingrained enough behaviour that we should support it, then there are a few options here, but it may need some careful discussion. |
@guybedford so the problem is that custom hooks should be able to change how the main entry point is resolved? |
Note that given that this is also true for the |
@guybedford do you see a problem with the following patch? diff --git a/lib/module.js b/lib/module.js
index e92c0b61d9..abc8006c3a 100644
--- a/lib/module.js
+++ b/lib/module.js
@@ -435,7 +435,7 @@ Module._load = function(request, parent, isMain) {
ESMLoader.hook(hooks);
}
}
- await ESMLoader.import(getURLFromFilePath(request).href);
+ await ESMLoader.import(getURLFromFilePath(request).pathname);
})()
.catch((e) => {
console.error(e); |
@targos yes that’s probably the easiest patch, but does mean accepting the somewhat unique concept of the top main being an absolute path and not an absolute url for the resolver. |
There's another nuance to this bug. It concerns files with no extensions. This will work: node ./node_modules/.bin/mocha But this won't: node --experimental-modules ./node_modules/.bin/mocha It fails with Not sure how to resolve this, but I'm assuming that for reasons of backward compatibility, once we are out of the experiment, then this MUST work and resolve to CJS. This is unfortunate, because a lot of people (including me!) use this "trick" of not having an extension to make it a Unix executable by adding a hash bang at the beginning, e.g.: |
@giltayar that pattern of Rather just use |
The reason is that absolute URLs do not go through extension and index checks. By switching to an absolute path, the resolver still applies extensions properly to the top-level main. PR-URL: #16526 Fixes: #16476 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
The reason is that absolute URLs do not go through extension and index checks. By switching to an absolute path, the resolver still applies extensions properly to the top-level main. PR-URL: #16526 Fixes: #16476 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
The reason is that absolute URLs do not go through extension and index checks. By switching to an absolute path, the resolver still applies extensions properly to the top-level main. PR-URL: #16526 Fixes: #16476 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
The reason is that absolute URLs do not go through extension and index checks. By switching to an absolute path, the resolver still applies extensions properly to the top-level main. PR-URL: nodejs/node#16526 Fixes: nodejs/node#16476 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
The reason is that absolute URLs do not go through extension and index checks. By switching to an absolute path, the resolver still applies extensions properly to the top-level main. PR-URL: nodejs/node#16526 Fixes: nodejs/node#16476 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
The reason is that absolute URLs do not go through extension and index checks. By switching to an absolute path, the resolver still applies extensions properly to the top-level main. PR-URL: nodejs/node#16526 Fixes: nodejs/node#16476 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Assuming the simple file
main.mjs
:Running
node --experimental-modules main.mjs
will work, butnode --experimental-modules main
won't work. You get:This is true even if the extension of the file is
js
.This is a regression - it works fine in
v8.7
.If it's a feature and not a bug, then it doesn't make sense to me, as importing the same file without an extension works fine - and I believe running a file in the command line and importing it should have similar semantics.
The text was updated successfully, but these errors were encountered: