Sub topic 8
How bundler resolution differs
Assume you already understand import, export, basic tsconfig.json, and the difference between CommonJS and ES modules. This chapter focuses on TypeScript projects whose code is processed by a bundler such as Vite, esbuild, Rollup, or webpack.
What bundler resolution means
Module resolution is the process of turning an import specifier into a file or package entry. For example, TypeScript must resolve this import:
import { greet } from "./greet";With moduleResolution: "bundler", TypeScript models the rules used by modern bundlers. Those rules are usually more flexible than Node.js runtime rules.
The typical configuration is:
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"noEmit": true
}
}noEmit tells TypeScript to check types without generating JavaScript. The bundler generates the final files instead. module: "preserve" keeps each import and export in its original form for the bundler to process.
Extensionless relative imports
Bundlers commonly allow an import to omit the source file extension:
import { greet } from "./greet";The bundler may try greet.ts, greet.tsx, or another configured extension. Bundler resolution lets TypeScript accept the same style.
This differs from nodenext. In a Node.js ES module, the emitted JavaScript usually needs the runtime-visible extension:
import { greet } from "./greet.js";The source file can still be greet.ts, because .js describes the file that will exist after compilation. An extensionless import that passes TypeScript's bundler checks may fail when executed directly by Node.js.
| Situation | Typical relative import | Main assumption |
|---|---|---|
| Bundler application | `./greet` | A bundler resolves and rewrites the import |
| Node.js ES module | `./greet.js` | Node.js resolves the emitted file directly |
| Package import | `some-library` | The package's metadata selects an entry |
Package exports and conditions
Bundlers understand the exports field in package.json. They can select an import entry for ES module code and often support conditions such as browser, development, or production.
TypeScript also uses package conditions while finding declarations. This helps the type checker follow the same public entry point that the bundler will use.
However, TypeScript does not run the bundler. A custom bundler plugin, alias, loader, or condition can change runtime behavior without TypeScript knowing about it.
What bundler mode does not do
Bundler mode does not make every import valid. The package must still expose the requested path, and the chosen file must have compatible types. It also does not automatically implement aliases at runtime.
For example, this configuration can type-check an alias:
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
}
}The bundler must separately be configured to understand @/. TypeScript's paths setting changes compiler lookup, not the JavaScript import that the browser or bundler receives.
A useful mental model
Choose bundler mode when the bundler owns module resolution and deployment. Choose nodenext when Node.js itself must resolve the emitted module graph, especially for a published package or a directly executed server application.
Exercise
Why can import "./greet" work with moduleResolution: "bundler" but fail when the emitted file runs directly as a Node.js ES module?
Show answer
Bundler mode assumes that a bundler can add extensions and choose source files. Node.js resolves the emitted JavaScript at runtime and generally requires the explicit .js extension in an ES module import.