-
Notifications
You must be signed in to change notification settings - Fork 12.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
Very confusing message when user doesn't realize they're in a CommonJS context #50009
Comments
This becomes especially confusing because it’s an ESM import you’re writing (so the error is actively wrong to say it “cannot be imported using this construct”—it can, with the right package configuration). The error only makes sense when you know the import is being transpiled to |
How about this. If the file is
Related diagnostics:
If the file does not have an
Related diagnostics:
If the file does not have an
Related diagnostics:
|
It's possible we could just have the related info be "resolved to this file". |
Is this useful? In an editor scenario you can already go-to-definition on it, but I don’t see what you’d need to do in the resolved file as part of addressing the error. |
I don’t think it’s appropriate to rule out dynamic import in any of these scenarios. I’m planning to use a single head message for all of them:
and optionally attaching either
or
(We cannot place a related diagnostic on the package.json file itself because it’s not a source file.) |
Sad - okay, what about - or add `\"type\": \"module\"` to its package.json file with path '{1}'.
+ or add the field `\"type\": \"module\"` to '{1}'. |
Actually, there is a |
I don't know if there's ever been a context where I could truly use a dynamic import in the context where I needed it. That's why I'm doubtful that it's helpful in the first place as a suggestion. That said, the related diagnostics can be smooshed. |
That may be true, but converting everything to ESM is a more consequential change and may be even more of a hassle. I also considered moving the dynamic import suggestion to a related diagnostic so it feels more like it has equal weight with the other suggestions:
|
Note that this isn't a valid fix if the tsconfig specifies |
I'm on Typescript 4.9.3 and this doesn't seem to be fixed yet. It is VERY confusing. I am glad that I found this thread, and hope the head message can be updated soon. |
It would be great if there was an option I could specify to silence this error. I'm importing ESM modules that I want typescript to recognize and process, but I'm not building with typescript. I'm using webpack, which handles all the import statements, which makes this error irrelevant noise. |
You're a life saver. Thank you. I did google, but I guess I figured it would've been something in the tsconfig. I appreciate your help. |
"compilerOptions": {
"moduleResolution": "node",
} Should do the trick too. |
That will not do the trick when importing ESM. |
Found from an issue I was diagnosing for @mpodwysocki.
Today, if you use
"module": "nodenext"
or"module": "node16"
, you might try to import from a package with ESM files and get the following error messageThis is extremely confusing. The fix is typically to add the following field to the
package.json
that corresponds to the importing file with the error:The text was updated successfully, but these errors were encountered: