-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
"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>
This still seems like a bug in Node 16.15.0. Why would we need to include the What am I missing here? |
I'm in the same page as @JoshMcCullough. As far as I got it from the previous comments, this problem would be solved and we wouldn't need to put the extension explicitly in our imports. I could solve this by adding Is there something that we can do to solve this? Node version: 16.15.0 Example: "scripts": {
"build": "npx tsc",
"start": "node --experimental-modules --es-module-specifier-resolution=node dist/index.js",
}, |
Config
|
What steps will reproduce the bug?
npm start
What is the expected behavior?
The script should display
Success!
, and does do so ifmypackage/Lib.js
is renamed tomypackage/index.js
.What do you see instead?
Additional information
I'm trying to run node with
-experimental-specifier-resolution=node
because TypeScript can't output .mjs files and I want to use extension-lessimport
statements. I prefer to useLib.js
instead ofindex.js
to 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: