Sub topic 8
How bundler resolution differs
This chapter assumes you have read the earlier chapters on how nodenext resolves imports and how package.json exports maps work. You should know the difference between a module's output format and the way the compiler finds a file, and you should be comfortable reading a tsconfig.json.
What bundler resolution is for
moduleResolution: "bundler" was added in TypeScript 5.0. It models the lookup rules of tools such as Vite, esbuild, webpack, and Rollup. Those tools do not run your code as Node.js would. They read your sources, follow imports, and write one or more output files. Because they do the file lookup themselves, they can be more relaxed than Node.js:
- They accept relative imports without a file extension.
- They resolve a directory to its
indexfile. - They do not decide per file whether the code is ESM or CommonJS.
- They still read
package.jsonexports, so packages from npm resolve as the bundler would resolve them.
TypeScript's job in this setup changes. It does not produce the files that run, so it acts as a type checker only. The bundler mode makes the compiler's lookup agree with what the bundler will do.
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"noEmit": true
}
}The preserve value for module (added in TypeScript 5.4) tells the compiler to keep import and export syntax as written. It also turns on esModuleInterop and allowSyntheticDefaultImports by default, and it selects bundler resolution if you leave moduleResolution out. Writing both options explicitly is clearer.
The decision flow
For each import, bundler mode takes a path that is shorter than the nodenext path, because it never asks what format the importing file has.
Relative imports without extensions
Under nodenext, a relative import inside an ES module file must name the emitted file, such as ./greet.js. Under bundler, the following import is valid:
// src/main.ts
import { greet } from "./greet";
import { formatDate } from "./utils"; // resolves ./utils/index.ts
console.log(greet("world"), formatDate(new Date()));TypeScript tries ./greet.ts, ./greet.tsx, and ./greet.d.ts. If none exists, it tries ./greet/index.ts and its siblings. This matches how bundlers behave, so the same source works in the editor, in tsc --noEmit, and in the bundle.
Writing ./greet.js also still works. TypeScript maps a .js specifier to the .ts source, as it does under nodenext. That makes code written for nodenext portable into a bundler project without edits.
Importing TypeScript extensions
Bundlers can also accept ./greet.ts as a specifier. TypeScript permits this only when it is not going to emit JavaScript, because the emitted import would still say .ts. The allowImportingTsExtensions option unlocks it, and it requires noEmit or emitDeclarationOnly.
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"noEmit": true,
"allowImportingTsExtensions": true
}
}Use this only if your toolchain supports it. Vite and esbuild do. A plain tsc build does not.
No per-file format detection
Under nodenext, the compiler decides whether each file is ESM or CommonJS from the file extension (.mts, .cts) and the nearest package.json "type" field. That decision changes which import syntax is legal, which exports condition is chosen, and whether extensions are required.
Under bundler, none of that applies. Every file is treated by the syntax it contains. A .ts file with import and export is a module, regardless of "type". The practical effects:
- A
"type": "module"field in your ownpackage.jsondoes not force extension rules on your sources. - You can mix
importand, with some options,import x = require("x")in the same project. - The
.mtsand.ctsextensions have no effect on resolution rules.
How `exports` is read
Bundler mode does read package.json exports in dependencies. It matches conditions in the order they appear in the map, and it uses a fixed set of conditions for an import statement:
| Condition | Matched in bundler mode |
|---|---|
| `types` | Yes, always |
| `import` | Yes, for `import` statements and `import()` |
| `require` | Only for `require`-style imports such as `import x = require("x")` |
| `default` | Yes, as the fallback |
| `node` | No |
| Custom names | Only if listed in `customConditions` |
Take the example library from the previous chapters:
{
"name": "example-lib",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
}
}When you write import { x } from "example-lib" in a bundler project, TypeScript picks the import branch and then types, so the types come from ./dist/index.d.ts. It never considers the require branch.
A bundler may add conditions that TypeScript does not know, such as browser, development, or worker. To make the type checker follow the same branch the bundler will, list them:
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"customConditions": ["browser"],
"noEmit": true
}
}If the bundler and the compiler disagree on conditions, you can get types from one build of a package and code from another. That mismatch is the most common hidden bug in bundler projects.
Packages without `exports`
If a dependency has no exports field, bundler mode falls back to the older rules: it looks at the types or typings field, then main, then an index file in the package root. Subpath imports such as lib/utils/thing then resolve as plain file paths inside the package folder. Once a package has an exports field, only the subpaths it lists are importable, and a deep import fails with an error that the module cannot be found.
Comparing the two modes
| Aspect | `nodenext` | `bundler` |
|---|---|---|
| Relative import extension | Required in ESM files | Optional |
| Directory `index` import | Not allowed in ESM files | Allowed |
| File format source | Extension and `package.json` `"type"` | Syntax only |
| `exports` conditions | `node`, `import` or `require` by file format | `import`, `types`, custom |
| Emits runnable code for Node.js | Yes, the intended use | Not intended, `noEmit` is typical |
| Required `module` value | `nodenext` | `preserve` or `es2015`+ |
Common errors
- "Cannot find module './x'" under
nodenextafter copying code from a bundler project. The extension is missing. Either add.jsto relative imports or switch the project tobundlerif a bundler runs it. - "Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later." The
modulevalue iscommonjsornode16. Change it topreserveoresnext. - Types resolve but the bundle fails. The conditions differ. Compare your bundler's condition list with
customConditions. - Deep import into a package fails. The package's
exportsdoes not list that subpath. Check the map rather than adding apathsworkaround.
You can see which condition and file TypeScript picked with tsc --noEmit --traceResolution. A later chapter covers this tool in detail.
Exercise
A project uses module: "preserve" and moduleResolution: "bundler". It imports example-lib, whose exports map is the one shown in this chapter. Which declaration file does TypeScript use for import { x } from "example-lib", and why? Then say what happens to the import ./greet if you change moduleResolution to nodenext and the file is in a "type": "module" package.
Show answer
TypeScript uses ./dist/index.d.ts. Bundler mode applies the import condition to import statements, and inside that branch the types condition is listed first, so it matches before default. The require branch is never considered. With nodenext in an ESM-format file, the extensionless relative import ./greet is an error because relative imports must include an extension. You would write ./greet.js.