-
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
TypeScript module "node16" does not work with CommonJS dependencies #49271
Comments
You are on the right track, this is related to Typescript being overly protective about importing CJS in an ESM file. The linked issue #49160 is the same idea. This is what typescript sees: https://github.com/axios/axios/blob/master/index.d.ts#L332 Axios is declaring a What typescript wants you do to is: import axios from 'axios';
await axios.default.get('https://google.com');
Axios is declaring the default, but it is also assigning the So it works as expected in Javascript. But they never told Typescript about it (missing an But you know it works that way, so you can do something like import axios, { Axios } from 'axios';
(axios as unknown as Axios).get('https://google.com'); and use it as normal. A little shameless self promotion, but the default-import package can help resolve this by properly resolving the default import + type in these weird cases import axios from 'axios';
import { defaultImport } from 'default-import';
defaultImport(axios).get('https://google.com'); TLDR this is definitely a known confusion, and is working as intended in Typescript. The actual issue (if one exists at all) would be in Axios' type definitions. Hopefully one of these three work-arounds will get you back up and running |
I think the problem is more than that. The issue I found is that I have this When in ESM, |
I think that is behaving as expected... The assertron library is written in CJS (TS allows it to be written as ESM, but the final result is outputted as CJS). There is an ESM version declared in the So the same issue comes up that CJS does not have a concept of I believe the |
@JacobLey thank you for the detailed explanation. I'm afraid this will make the full transition to ESM long, as many packages that work well with TS and module=commonjs will not get updated soon... |
The reason is because axios "lied" about what it exports in the declaration. TS doesn't know that |
Unfortunately we don't have any good ways to force external packages to be correctly configured, and the overall design of node modules means that range of misconfigurations we're able to account for isn't very accommodating. |
just a shot in the dark: maybe typescript could help those external packages to configure themselves correctly somehow? |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
I find it strange that TS knows that that there is a I agree with @m-radzikowski that this is a major friction point, and breaks the existing ecosystem. This should at least be handled by another optional interop mode. Surely can implemented similar to @JacobLey's library. |
Bug Report
π Search Terms
esm, cjs, axios
π Version & Regression Information
TS 4.7.2
β― Playground Link
Repository: https://github.com/m-radzikowski/ts-issue49271
π» Code
tsconfig.json:
index.ts:
Run:
tsc
π Actual behavior
Error:
π Expected behavior
My understanding is the module "node16" introduced in TS 4.7 is meant to enable having the project use ESM while still being interoperable with CJS. Just like Node.js 16 is. Thus the ESM project could use CommonJS dependencies.
In this case, axios is a CommonJS dependency, without ESM dist. So I thought TS should handle it fine?
Contrary, the module "ES2022" added in TS 4.5 is a strict-ESM option, that requires the whole project to be strictly ESM, including dependencies. At least that was my understanding of the difference between those two settings.
#49160 was similar but there the conclusion was that the libraries were misconfigured. I'm not sure that's the case here - axios package.json has:
and no
exports
etc., which makes it a pure CJS package.I may be (and probably I am) wrong somewhere here, so I would be glad for an explanation of that behavior.
The text was updated successfully, but these errors were encountered: