"Cannot find module" when main file not index.js with experimental-specifier-resolution=node
#32103
Comments
|
/cc @nodejs/modules |
|
The repro link seems correct to me; if so, this seems like a bug. If you use explicit extensions, and don't use experimental specifier resolution, what happens? |
TypeScript has only limited support for ES modules so far. But in many cases the following workaround works:
(That's the more verbose version of Jordan's "If you use explicit extensions". :)) |
Then the script runs correctly. |
|
So the issue appears to be that to support that we introduced for https://github.com/nodejs/node/blob/master/src/module_wrap.cc#L1180-L1186 This is where it seems like where we are doing the resolution itself https://github.com/nodejs/node/blob/master/src/module_wrap.cc#L826-L862 It is possible that some order of operations bug is not even checking for the package.main... and tbh the work we've done around exports is going to confuse this a bit too. I'll try and find some time to dig in but this is going to have to be lower priority for me personally, so if anyone else wants to pick this up please go ahead! as an aside, thanks so much for these amazing bug reports @dandv |
|
I figured out the exact reason. function moduleResolve(specifier /* string */, base /* URL */) { /* -> URL */
// Order swapped from spec for minor perf gain.
// Ok since relative URLs cannot parse as URLs.
let resolved;
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
resolved = new URL(specifier, base);
} else {
try {
resolved = new URL(specifier);
} catch {
return packageResolve(specifier, base);
}
}
return finalizeResolution(resolved, base);
}The parameter I am working on this, maybe I will submit a PR in about a few days. |
|
EDIT: This is working as intended, I misunderstood the spec. See https://nodejs.org/api/esm.html#esm_import_specifiers Leaving the below for history only. Seeing the same thing on Node 14.1, OSX. In my package.json I have I then have one file, server.js serverModule.js Now, I notice that adding But then in turn it complains about
|
|
Thanks! This solution worked for me. |
|
This seems resolved, closing. Feel free to reopen if something needs to be discussed further, thank you. |
|
@ryzokuken I believe this actually still remains an implementation bug and I believe my suggestion in #32612 (comment) might be related to the fix here. That said, we might end up deprecating this flag before the fix at this rate... |
|
Where's the solution after this much of comments and showing expertise? No solution is found in github forum. Yall show how much yall know. I come here often to github to see some solutions to the problem. There's always talking. Please provide the solutions in simple words my experts. Some people are naive out there. Not everybody pros like yall |
|
The solution is to upgrade to Node.js v16.4.0 or later, or wait for v14.17.4 once it is released next week. I'll try to backport it to v12.x as well. |
PR-URL: nodejs#38979 Fixes: nodejs#32103 Fixes: nodejs#38739 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com>
PR-URL: nodejs#38979 Fixes: nodejs#32103 Fixes: nodejs#38739 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com>
PR-URL: nodejs#38979 Fixes: nodejs#32103 Fixes: nodejs#38739 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com>


What steps will reproduce the bug?
npm startWhat is the expected behavior?
The script should display
Success!, and does do so ifmypackage/Lib.jsis renamed tomypackage/index.js.What do you see instead?
Additional information
I'm trying to run node with
-experimental-specifier-resolution=nodebecause TypeScript can't output .mjs files and I want to use extension-lessimportstatements. I prefer to useLib.jsinstead ofindex.jsto distinguish in my IDE between the main files of multiple packages in my monorepo that otherwise would all look likeindex.js.The text was updated successfully, but these errors were encountered: