Sub topic 12
Choosing nodenext or bundler
This chapter helps you pick nodenext or bundler for a project. It assumes you can read a tsconfig.json, you know that Node.js runs both CommonJS and ES modules, and you have seen the module and moduleResolution options. The earlier chapters explained how each mode resolves imports. Here the focus is the decision: what question to ask, what each answer costs, and which mistakes to avoid.
The deciding question
The choice depends on one fact: what program reads your emitted JavaScript first?
- If Node.js runs the files directly, or a library you publish may be run by Node.js directly, use
nodenext. - If a bundler such as Vite, esbuild, webpack, or Rollup processes every import before anything runs, use
bundler.
TypeScript does not run your code. It predicts how another tool will find files. A wrong prediction produces the worst kind of bug: the compiler accepts an import and the runtime rejects it, or the reverse.
What each mode assumes
The two modes differ in a few concrete rules.
| Concern | `nodenext` | `bundler` |
|---|---|---|
| Relative import extensions in ES module files | Required, such as `./greet.js` | Optional, `./greet` works |
| Directory imports such as `./utils` meaning `./utils/index` | Not allowed in ES module files | Allowed |
| Format of each file | Decided per file by the `.mts`/`.cts` extension and the nearest `package.json` `"type"` | Not decided by TypeScript, the bundler handles it |
| `exports` map in packages | Read, with `import` or `require` condition depending on the importing file | Read, always with `import` condition (plus `types`, `default`) |
| Emits JavaScript | Yes, usually | Usually not, `noEmit: true` |
| Allowed `module` value | `nodenext` (and it sets the resolution) | `preserve` or `es2015` and later, with `moduleResolution: "bundler"` |
Two consequences follow from the table.
First, nodenext is strict because Node.js is strict. Node does not guess extensions in ES modules. If TypeScript allowed ./greet there, the compiled code would crash on start.
Second, bundler is permissive because bundlers are permissive. They search for extensions and directory index files, so TypeScript does too. The cost is that your source is no longer valid for plain Node.js.
The two configurations side by side
For a program or library that Node.js runs:
{
"compilerOptions": {
"module": "nodenext",
"target": "es2022",
"strict": true,
"outDir": "dist"
}
}Setting module to nodenext also selects nodenext resolution, so you do not set moduleResolution separately.
For an application processed by a bundler:
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"noEmit": true
}
}With noEmit, TypeScript only type-checks. The bundler produces the output.
Decision criteria
Choose bundler for applications behind a bundler
Pick bundler when all of these hold:
- A bundler or a bundling dev server processes the whole import graph.
- You do not run the compiled output with plain Node.js.
- You want extensionless imports and directory imports, which most front-end templates use.
Typical cases are a React or Vue single-page app built with Vite, or a Next.js project. Here nodenext would force you to write .js extensions that the bundler does not need, and it would make you decide a format for every file, even though the bundler ignores that decision.
Choose nodenext for code Node.js loads
Pick nodenext when any of these hold:
- You run
node dist/main.jsor a similar command. - You write a command-line tool, a server, or a script that runs without a bundler.
- You publish a package to npm that consumers may load in Node.js.
Choose nodenext for libraries, even if you bundle your own app
A library is the case where the two modes disagree most, and the choice is not symmetric. Code that compiles under nodenext almost always works under bundler too, because nodenext is the stricter set of rules. The reverse is false. A library written under bundler can contain import "./utils", which compiles, and which then breaks for any consumer who runs the emitted files under Node.js.
So a library should be checked under nodenext even if you test it only through a bundler. This is the practical rule: write to the strictest mode your consumers might use.
Edge cases
Node.js with a transpile step
Some projects run TypeScript with a loader or a runtime that strips types and runs the file immediately. Whether nodenext or bundler fits depends on how that tool resolves imports. If it follows Node.js rules, including requiring extensions, use nodenext. If it uses its own search, you need to read its documentation. Do not assume bundler just because no tsc emit step exists.
Mixed repositories
A monorepo can hold a Vite app and a Node.js library. Do not force one setting on both. Give each package its own tsconfig.json, possibly extending a shared base that holds only options both modes accept, such as strict.
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "nodenext",
"outDir": "dist"
}
}The app package extends the same base and sets module: "preserve" and moduleResolution: "bundler" instead.
Dependencies decide too
Your choice affects which entry of a dependency's exports map TypeScript reads. Under nodenext, an ES module file asks for the import condition and a CommonJS file asks for require. Under bundler, TypeScript always uses import. A package that ships different types for each branch can therefore type-check in one mode and fail in the other. If a dependency's types look wrong after you switch modes, inspect its exports map before blaming your configuration.
The compiler cannot see the bundler's settings
bundler resolution trusts that your bundler resolves the same way. If you configure aliases or custom conditions in the bundler, TypeScript does not read them. You must mirror them in paths or customConditions, or the type checker and the bundler will disagree.
Common mistakes
- Using
bundlerfor a library because the app that tests it uses a bundler. The library compiles and then fails for Node.js consumers. Check the library undernodenext. - Using
nodenextin a front-end app and fighting the extension errors. If a bundler loads your code, switch tobundlerinstead of adding.jseverywhere. - Setting
moduleResolution: "nodenext"with a differentmodulevalue. TypeScript requires the two to match fornodenext. Set onlymodule: "nodenext". - Treating the choice as a style preference. It encodes a fact about your runtime. Revisit it if you add or remove a bundler.
- Mixing the two modes in one package and expecting the same output. Emit settings, extensions, and the
exportsbranch that is read all change together.
Checking your choice
After choosing, validate it with the compiler instead of guessing. For a library, run the type check under nodenext even if your day-to-day configuration is different:
npx tsc --noEmit --module nodenext
npx tsc --noEmit --explainFilesIf the first command reports missing extensions or unresolved packages, those same problems would reach Node.js consumers. The second command shows why each file is in the program, which helps when a mode change pulls in an unexpected declaration file.
Summary of the rule
Ask who loads the emitted files first. A bundler means bundler for applications. Node.js means nodenext. A library means nodenext, because the strictest mode catches the problems that any consumer might hit.
Exercise
Your team has a Vite single-page app that is never run by plain Node.js, and a shared utility library in the same repository that is published to npm and also imported by the app. Which mode should each package use, and why is a single shared setting a poor choice?
Show answer
The app should use module: "preserve" with moduleResolution: "bundler" and noEmit: true, because Vite resolves every import and extensionless imports are convenient. The published library should use module: "nodenext", because Node.js consumers may load its emitted files directly, and nodenext is the stricter mode that rejects extensionless imports that would break at runtime. A single shared setting is poor because bundler would let the library ship imports that fail in Node.js, and nodenext would force the app to use .js extensions and per-file format decisions that Vite does not need. Share only a base config for common options such as strict, and let each package set its own module and moduleResolution.
Exercise
A library compiles cleanly under moduleResolution: "bundler", but a user reports ERR_MODULE_NOT_FOUND when running it in Node.js. What is the most likely cause, and how do you confirm it?
Show answer
The most likely cause is a relative import without a file extension, such as import "./utils", or a directory import. bundler accepts both, but Node.js does not resolve them in ES modules. Confirm it by running npx tsc --noEmit --module nodenext, which reports the same imports as errors. Fix them by writing ./utils.js or ./utils/index.js, and keep the library's configuration on nodenext.