Sub topic 3
The `module` and `moduleResolution` options
Assume you are comfortable with TypeScript import and export statements, basic tsconfig.json settings, and running tsc. You should also know that Node.js supports both CommonJS and ES modules.
Two TypeScript compiler options control different parts of the module system:
moduledescribes the module format TypeScript should preserve or emit.moduleResolutiondescribes how TypeScript finds the file or package named by an import.
These options work together, but they answer different questions.
| Option | Main question | Example values |
|---|---|---|
| `module` | What module format should the output use? | `nodenext`, `commonjs`, `preserve` |
| `moduleResolution` | Where should an import be found? | `nodenext`, `node10`, `bundler` |
For example, when TypeScript sees import { greet } from "./greet.js", module resolution locates the source file and its declarations. The module setting determines how that import appears in emitted JavaScript.
Choosing `nodenext` for Node.js
Use nodenext when Node.js will run the generated files directly. This setting models current Node.js behavior, including package boundaries, file extensions, package.json conditions, and the difference between CommonJS and ES modules.
Setting module to nodenext also selects nodenext module resolution. The compiler therefore uses matching rules for both output and lookup.
{
"compilerOptions": {
"module": "nodenext",
"target": "es2022",
"strict": true,
"outDir": "dist"
}
}Under this configuration, TypeScript examines the nearest package.json and the file extension to decide whether each source file is an ES module or a CommonJS module. A package with "type": "module" treats ordinary .ts files as ES module source. A package without that field generally treats them as CommonJS source. The .mts and .cts extensions can explicitly select ES module and CommonJS behavior.
Because Node.js requires extensions in ES module relative imports, this source uses .js even though the source file is named greet.ts:
// src/main.ts in a package whose package.json has "type": "module"
import { greet } from "./greet.js";
console.log(greet("world"));TypeScript understands that ./greet.js refers to the emitted JavaScript file and checks the corresponding TypeScript source.
Choosing `bundler`
Use bundler when another tool, such as Vite, esbuild, or webpack, processes the output. Bundlers often support extensionless relative imports and additional package conventions that Node.js itself does not use.
A common application configuration is:
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"noEmit": true
}
}module: "preserve" keeps each import and export in its original module form. The bundler then decides how to combine and transform the modules. noEmit tells TypeScript to type-check the project without writing JavaScript.
With bundler resolution, this may be valid:
import { greet } from "./greet";The bundler can try extensions such as .ts, .tsx, or .js. That convenience can hide a deployment problem if the same code is later run directly by Node.js.
Keep the pair consistent
The most common mistake is choosing bundler for a project that Node.js runs directly, or choosing nodenext for source that a bundler intentionally transforms.
When a project uses a published package, test it in the environment that will consume it. Libraries may need separate builds and declaration files for Node.js import, Node.js require, and bundler consumers.
Exercise
Which pairing is the better default for TypeScript source that a bundler processes without TypeScript emitting JavaScript: module "preserve" with moduleResolution "bundler", or module "commonjs" with moduleResolution "nodenext"?
Show answer
module "preserve" with moduleResolution "bundler". It leaves module syntax for the bundler and uses lookup rules that match bundler behavior.