Sub topic 12
Choosing `nodenext` or `bundler`
This chapter assumes that you can write TypeScript imports and exports, edit tsconfig.json, and run tsc. You should also know that Node.js supports both CommonJS and ES modules.
The moduleResolution option tells TypeScript how to turn an import such as ./math.js or lodash into a file and its type declarations. The module option tells TypeScript which module format to preserve or emit. These options work together.
Choose nodenext when Node.js will load your compiled files directly. Choose bundler when another tool, such as Vite, esbuild, or webpack, will process the imports before the application runs.
What each mode models
nodenext follows current Node.js behavior. It checks package boundaries, the type field in package.json, file extensions, and conditional exports entries. It can model both CommonJS and ES modules in the same project.
bundler models the rules used by modern bundlers. Bundlers commonly accept extensionless relative imports and resolve extra file types or package entry points before producing a browser or server bundle. TypeScript checks the source using those assumptions and normally does not emit JavaScript.
| Situation | Recommended mode | Reason |
|---|---|---|
| Node.js runs the emitted files directly | `nodenext` | TypeScript checks imports using Node.js runtime rules |
| Vite or esbuild builds an application | `bundler` | The bundler performs the final import resolution |
| A library must work in Node.js and bundlers | Usually `nodenext` while testing published entries | It exposes runtime and package-boundary mistakes |
| A browser library is consumed only through a known bundler | `bundler` for the application, with separate library publishing checks | The application and published package have different concerns |
Configuration for a Node.js application
Use nodenext when the output directory will be run by Node.js without a bundling step.
{
"compilerOptions": {
"module": "nodenext",
"target": "es2022",
"strict": true,
"outDir": "dist"
},
"include": ["src"]
}With this setting, TypeScript decides whether each file is ESM or CommonJS using its extension and the nearest package.json. For example:
- A
.mtsfile is always an ES module. - A
.ctsfile is always a CommonJS module. - A
.tsfile follows the nearest package's"type"field. - In a package with
"type": "module", a.tsfile is treated as ESM. - In a package without that field, a
.tsfile is treated as CommonJS.
An ESM file must use the extension that the emitted JavaScript will have:
// src/main.ts in a package with "type": "module"
import { greet } from "./greet.js";
console.log(greet("world"));The source file is greet.ts, but the import says greet.js because Node.js will load dist/greet.js after compilation. An extensionless import might compile successfully in another mode but fail when Node.js executes it.
Configuration for a bundled application
Use bundler when a bundler owns the final module graph.
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"noEmit": true
},
"include": ["src"]
}noEmit prevents tsc from producing JavaScript. The bundler reads the TypeScript source, resolves imports, transforms the files, and creates the deployable output.
Bundler mode permits patterns that Node.js would reject at runtime, such as this extensionless import:
import { greet } from "./greet";
console.log(greet("world"));This is useful only when the configured bundler supports the same pattern. TypeScript can verify the code, but it cannot make a runtime loader understand an import that the loader does not support.
A practical decision process
Ask where the import resolution happens:
- If Node.js resolves imports after compilation, use
nodenext. - If a bundler resolves imports during a build, use
bundler. - If different consumers will load a published package in different ways, test each public entry point with its intended condition.
- If the answer changes between development and production, align development checks with the production loader instead of hiding the difference.
A library may use a bundler to build its files, but that does not make its package configuration correct. Its exports map still needs valid targets for both import and require when both are supported. Test the installed package from a small ESM project and a small CommonJS project.
Exercise
A Vite application uses extensionless relative imports and sets noEmit to true. Should its TypeScript configuration normally use nodenext or bundler, and why?
Show answer
It should normally use bundler because Vite resolves the imports during the build, while TypeScript only type-checks the source.