Skip to content

Extension required when moduleResolution=bundler and imports/exports used #63157

@polson136

Description

@polson136

🔎 Search Terms

file extension bundler imports export

🕗 Version & Regression Information

  • This is the behavior in every version I tried (6.0.0-dev.20260218, 5.9.3, 5.6.2), and I reviewed the FAQ for entries about common-bugs-that-arent-bugs

⏯ Playground Link

No response

💻 Code

with moduleResolution=bundler

src/example.ts

import internalFoo from '#src/imported-file'; // fails unless ".js" is added
import externalFoo from 'ts-repro/imported-file'; // fails unless ".js" is added
import relativeFoo from './imported-file'; // works

console.log(internalFoo, externalFoo, relativeFoo);

src/imported-file.ts

export default 'foo';

tsconfig.json

{
  "compilerOptions": {
    "noEmit": true,
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
  },
  "include": ["src/**/*.ts"]
}

package.json

{
  "name": "ts-repro",
  "version": "1.0.0",
  "scripts": {
    "check": "tsc"
  },
  "imports": {
    "#src/*": "./src/*"
  },
  "exports": {
    "./*": "./src/*"
  },
  "packageManager": "pnpm@10.22.0",
  "devDependencies": {
    "typescript": "6.0.0-dev.20260218"
  }
}

🙁 Actual behavior

pnpm check

> ts-repro@1.0.0 check /home/polson/drw/ts-repro
> tsc

src/example.ts:1:25 - error TS2307: Cannot find module '#src/imported-file' or its corresponding type declarations.

1 import internalFoo from '#src/imported-file';
                          ~~~~~~~~~~~~~~~~~~~~

src/example.ts:2:25 - error TS2307: Cannot find module 'ts-repro/imported-file' or its corresponding type declarations.

2 import externalFoo from 'ts-repro/imported-file';
                          ~~~~~~~~~~~~~~~~~~~~~~~~


Found 2 errors in the same file, starting at: src/example.ts:1

🙂 Expected behavior

tsc passes, which aligns with the behavior of vite build:

Image

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>ts-repro</title>
  </head>
  <body>
    <script type="module" src="/src/example.ts"></script>
  </body>
</html>

</code>

vite.config.mts

import { defineConfig } from 'vite';

export default defineConfig({});

Additional information about the issue

The reason why I care is that I'm writing a react app with a mixture of .ts and .tsx files, so I can't just use "./src/*.ts" because tsx files won't match

Typescript docs

  • According to the docs: "--moduleResolution bundler attempts to model the module resolution behavior common to most JavaScript bundlers". There is clearly a divergence between the bundler behavior and the typescript behavior
  • The module resolution docs say that "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", which is wonderfully ambiguous about this edge case
  • I found one doc that says TypeScript’s implementation for resolving a module specifier through "exports" to a file path follows Node.js exactly, which seems to agree that the "actual behavior" described above is the desired behavior, but this seems really weird. I would expect it to match the bundler behavior when moduleResolution=bundler

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions