Sub topic 5
Choosing the module compiler option
This chapter assumes you know basic TypeScript types, JavaScript import and export, tsc, and package.json. You should also understand that a runtime, such as Node.js or a browser bundler, loads JavaScript modules according to its own rules.
What `module` controls
The TypeScript module option tells the compiler which JavaScript module format to emit. It also affects how TypeScript interprets some module settings and package metadata.
The two main formats are:
- ES modules (ESM) use
importandexport. - CommonJS (CJS) uses
requireandmodule.exports.
For example, TypeScript can leave this source as ESM:
import { add } from "./math.js";
console.log(add(2, 3));With a CommonJS setting, TypeScript emits code using require. With an ESM setting, it emits import. The source syntax can look the same, but the generated JavaScript must match the environment that runs it.
Choose according to the target
| Target environment | Typical `module` value | Typical `moduleResolution` |
|---|---|---|
| Modern Node.js project | `nodenext` | `nodenext` |
| Older Node.js CommonJS project | `commonjs` | `node10` or the project default |
| Browser or bundler using ESM | `es2022` or `esnext` | `bundler` or an explicit compatible strategy |
| Bundler that preserves each module form | `preserve` | `bundler` |
nodenext is usually the safest choice for a current Node.js application. It follows Node.js rules for .js, .mjs, .cjs, package.json "type", and package exports. It can emit either ESM or CommonJS for different files when the project uses Node.js conventions.
A bundler usually performs the final module transformation. In that case, module: "preserve" keeps import and export statements close to the original source, while moduleResolution: "bundler" models the way a bundler finds packages and files.
A Node.js ESM configuration
For a Node.js project that uses ESM, set the package type and use nodenext:
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"moduleResolution": "nodenext",
"strict": true,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}{
"type": "module"
}With this configuration, a source file can be:
// src/math.ts
export function add(a: number, b: number): number {
return a + b;
}
// src/main.ts
import { add } from "./math.js";
console.log(add(2, 3));The .js extension in the source import is intentional. Node.js loads the emitted file as dist/math.js, even though TypeScript checks the source file src/math.ts. Omitting the extension may cause the compiled program to fail at runtime.
Common mistakes
A frequent error is choosing module: "commonjs" for code that will run as ESM. The emitted require calls then conflict with the runtime.
Another error is using module: "nodenext" while following bundler conventions such as extensionless imports or aliases. Use moduleResolution: "bundler" only when a bundler, rather than Node.js directly, owns module loading.
Do not choose a setting because it is the newest value. Choose the setting that matches the final loader.
Exercise
A project is bundled for the browser, and the bundler expects to process import and export statements itself. Which module settings are a suitable starting point?
Show answer
Use module: "preserve" and moduleResolution: "bundler", then adjust them if the bundler's documentation requires another compatible configuration.