Skip to content

"moduleResolution": "bundler" incorrectly allows default imports from commonjs modules #55681

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

Closed
DetachHead opened this issue Sep 8, 2023 · 2 comments

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Sep 8, 2023

πŸ”Ž Search Terms

moduleresolution bundler default import commonjs

πŸ•— Version & Regression Information

5.3.0-dev.20230908

⏯ Playground Link

No response

πŸ’» Code

my code

tsconfig.json
{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "esModuleInterop": true,
    "strict": true
  },
  "include": ["main.ts"]
}
main.ts
import foo from './foo.cjs'

console.log('this works at runtime:', foo.default.a)

third party commonjs package

foo.cjs
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = { a: 1 };
foo.d.cts
declare const _default: {
    a: number;
};
export default _default;

πŸ™ Actual behavior

> tsc; node main
main.ts:3:43 - error TS2339: Property 'default' does not exist on type '{ a: number; }'.

3 console.log('this works at runtime:', foo.default.a)
                                            ~~~~~~~


Found 1 error in main.ts:3

this works at runtime: 1

πŸ™‚ Expected behavior

no compile error, because at runtime it's not a real default export, but rather an object with a property called default on it

Additional information about the issue

changing moduleResolution to nodenext fixes the issue, but i don't think this is intended behavior because according to the docs the only thing that should be different to node16/nodenext is that it doesn't enforce the file extensions:

'bundler' for use with bundlers. Like node16 and nodenext, this mode supports package.json "imports" and "exports", but unlike the Node.js resolution modes, bundler never requires file extensions on relative paths in imports.

@Andarist
Copy link
Contributor

Andarist commented Sep 8, 2023

This might be a duplicate of #54102

@DetachHead
Copy link
Contributor Author

dang that was fast lol

@DetachHead DetachHead closed this as not planned Won't fix, can't repro, duplicate, stale Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants