-
Notifications
You must be signed in to change notification settings - Fork 12.8k
"moduleResolution": "node12"
of Typescript 4.5 does not work as expected
#46408
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Issue template for bug reports. Although this looks more like a question, better suited for sites like StackOverflow. |
It is created by |
|
@weswigham Thanks for reply. I tried the following locally for {
"exports": {
"import": "./lib/index.js",
"require": "./lib/index.cjs",
"types": "./lib/index.d.ts"
}
} This still does not work, is there anything wrong?
But I'm using Did you mean something like |
- Requires Typescript to be fixed for 4.5. microsoft/TypeScript#46408
I tried to use
I've tried the following in {
"exports": {
"import": "./lib/index.js",
"require": "./lib/index.cjs",
"types": "./lib/index.d.ts"
}
} I still don't understand how to fix this part. See https://github.com/JounQin/test/tree/ts_esm for reproduction |
|
(But actually, since there's a separate cjs and esm entry point, I'd delete the |
I've made a note to discuss options for validating / issuing suggestions/diagnostics on package.json files, because that seems like a very easy mistake to make. In the meantime, it sounds like things are working as expected here. @weswigham correct me if I’m wrong, but I think we can close this? @JounQin questions/discussion is welcome to continue, just trying to parse this to see if we need to assign out any immediate work. |
More direct discussion of export map priorities being a footgun at #46334 |
Yeah, I don't think there's anything actionable for us here. |
I tried {
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"exports": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.cjs"
},
"types": "./lib/index.d.ts"
} But it still does not work. So |
- Requires Typescript to be fixed for 4.5. microsoft/TypeScript#46408
From 4.7 rc's release notes (for refernce)// package.json
{
"name": "my-package",
"type": "module",
"exports": {
".": {
// Entry-point for `import "my-package"` in ESM
"import": {
// Where TypeScript will look.
"types": "./types/esm/index.d.ts",
// Where Node.js will look.
"default": "./esm/index.js"
},
// Entry-point for `require("my-package") in CJS
"require": {
// Where TypeScript will look.
"types": "./types/commonjs/index.d.cts",
// Where Node.js will look.
"default": "./commonjs/index.cjs"
}
}
},
// Fall-back for older versions of TypeScript
"types": "./types/index.d.ts",
// CJS fall-back for older versions of Node.js
"main": "./commonjs/index.cjs"
} It seems that all exports CJS exports using the So the following doesn't work: "exports": {
".": {
"types": "./index.d.ts",
"import": "./index.mjs",
"require": "./index.cjs"
}
} Nor does this "exports": {
".": {
"import": {
"types": "./index.d.ts",
"default": "./index.mjs",
},
"require": {
"types": "./index.d.ts",
"default": "./index.cjs",
}
}
} Which means that if you want to use the same types for both your ESM and CJS exports, you'll need to make a dummy type file like this: export * from "./index";
export { default } from "./index"; Edit: That work around doesn't work. You'd need to fully dupe the whole file and all sub files. Wouldn't it be nicer to just allow |
Nope! The format of the file is important information to the type system - it tells us how the module is loaded and, importantly, if there's a default that's the shape of the module itself when imported in esm, and if it's an error to load it at all in cjs. At runtime a file can't be both an esm and cjs module, and thus neither can the types for a module. (Though, as you've observed, nothing stops you from pulling almost the whole definition of one of the formats from the other one, in the same way you can re-export the actual implementation!) |
I tried this and all though TS no longer complains about the initial import; within that file TS complains about the imports and refuses to resolve them, so you just end up with |
Is that not just because node es module imports require full paths and explicit file extensions (likely .js) to resolve? |
So I have the following types pre 4.7: // index.d.ts
export { default } from "./foo";
export * from "./bar"; // type only exports I tried making the following types for 4.7 to go along side them: // index.d.mts
export { default } from "./index.js";
export * from "./index.js"; // index.d.cts
import Foo from "./index.js";
export = Foo; But when using TypeScript 4.7-rc, TS complains about the exports (imports) in Also, when using
|
|
But every version of ts ever (that has a commonjs resolver) actually allows keeping the (js) extension on the import, so you probably don't need to do much other than add extensions. |
Oki, that's good to know. It my be worth mentioning that in 4.7's release notes as I imagine quite a few other people will also have this misunderstanding as up until now, extensions have pretty much always been left off for TS/JS file imports. |
I just tried the
typescript@next
andmoduleResolution: 'node12'
with@angular/compiler@v13
, buttsc -b
failed to build:@angular/compiler@v13
is ESM only,ParsedTemplate
is typing exported from itstypes
entry.See https://unpkg.com/browse/@angular/compiler@13.0.0-rc.0/package.json
synckit
is both commonjs and ESM compatible.See https://unpkg.com/browse/synckit@0.6.0/package.json
I have no idea how can it be fixed on my side.
Test source codes:
Originally posted by @JounQin in #45884 (comment)
The text was updated successfully, but these errors were encountered: