-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 import a package with "type": "module"
from cjs even if the package has an exports
map
#49299
Comments
So the issue happening here is that the line An alternative fix (for the library) is to make
While it does seem like it would be reasonable for export default function foo(): number; If this declaration is interpreted as ESM, the intention is clear, however interpreted as CJS, is this supposed to be a CJS module with Both interpretations seem reasonable, and there's no way to tell just from the declaration which it is. If the library is actually implemented in TS, then the better thing for the library to do is just to have |
If I hover over |
TypeScript should just treat it as |
Note that from TS's perspective, there is no CJS module, this is because when Even though Hence it shows you the types for the ES module, but it considers it an error as |
So basically the issue is that by the dependency specifying So if If this is indeed the case, is it consider safe to take this approach or should a library always give both a |
No then it will be interpreted as commonjs, however ESM can import so there won't be an error reported. However, this isn't without hazards, i.e. consider if we had like I suggested above, however we also had an ESM file: import foo from "my-dep";
console.log(foo); Under the CJS resolution the shape of the module is { default: function foo() { ... } } This is probably not what was intended, because in ESM we would want the program to print the function itself: function foo() { ... } (Note that this is actually what the runtime does, because there actually is an ESM file). This is unlike prior TS code, because in prior TS code there was esModuleInterop, which would've extracted the default export from the CJS namespace. But actual Node ESM doesn't extract the default export, it always returns the whole CJS namespace.
Yes. It is kind've a shame that the files would be identical, although given identical contents is ideal for compression there's not really any loss to it. |
Oki, that makes sense. What about in the case of a JS library providing it's own type declarations? Would the following be valid?
import type { Foo } from './types/foo.js';
import { Bar } from './types/bar.js';
export {
Foo,
Bar as default,
};
export { Foo } from "./index.js";
export default from "./index.js";
import Bar from "./index.js";
export = Bar; Or would all the |
Yes, this pattern is fine. |
@Jamesernator from my testing, it seems to fail. With #47807, I believe it's possible to make it work but I haven't tested too much. Also with regard to |
Ah yeah, you probably need
Kind've, you can As an example if the CJS declare function Bar(): number;
declare namespace Bar {
export type Foo = {};
}
export = Bar; And this will allow |
@Jamesernator Thanks for your help with this. |
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
I was unable to test this on prior versions because it's a brand new feature.
⏯ Playground Link
N/A
💻 Code
Code:
tsconfig.json
:Dependency's
package.json
:🙁 Actual behavior
Module 'my-dep' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead. ts(1471)
🙂 Expected behavior
TypeScript should be able to find the right import of
my-dep
from itsexports
map. Thetype
field shouldn't prevent this.Extra info
If I do any one of the following, everything works as expected.
"type": "module"
in my ownpackage.json
"type": "module"
frommy-dep
'spackage.json
.mts
No changes to
tsconfig.json
seem to fix the issue.I would have expected that the following would have worked but it didn't:
The text was updated successfully, but these errors were encountered: