Sub topic 3
The module and moduleResolution options
This chapter assumes you can write TypeScript with import and export, run tsc with a tsconfig.json, and know that Node.js runs both CommonJS and ES modules. It covers the two compiler options that control how imports are handled, and how they interact.
Two questions, two options
TypeScript has to answer two separate questions about every module in your program.
- When it reads
import { greet } from "./greet.js", which file on disk does that specifier mean? This is module resolution. - When it writes JavaScript for your file, should the output use
importandexport, orrequireandmodule.exports? This is the module output format.
Each question has its own option:
| Option | Question it answers | Affects |
|---|---|---|
| `moduleResolution` | Which file does this specifier point to? | Type checking, and which files join the program |
| `module` | What module syntax goes into the emitted JavaScript, and how is each file's format decided? | Emitted `.js` files, and some checking rules |
The two are related, but they are not the same thing. A project can resolve imports one way and emit another format. Most confusing errors in this area come from mixing the two questions up.
What module controls
The module option sets the module system of the emitted code. Common values include:
commonjs: emitrequireandexports.es2022oresnext: emitimportandexportas written.node16andnodenext: emit ESM or CommonJS per file, following Node.js rules.preserve: keep the import and export syntax in your source as it is, and let a later tool deal with it.
With commonjs or es2022, every file gets the same output format. With node16 and nodenext, the format can differ from one file to the next, because Node.js itself decides that file by file. The next chapter sections cover how that decision is made. Here the point is that nodenext is not a single output format. It is a rule for choosing one.
What moduleResolution controls
The moduleResolution option picks the algorithm that turns a specifier into a file. The values you will meet are:
| Value | Models | Typical use |
|---|---|---|
| `node16` / `nodenext` | Node.js with ESM and CommonJS support | Code that Node.js runs directly |
| `bundler` | A bundler such as Vite, esbuild, or webpack | Code that a bundler processes |
| `node10` (also called `node`) | Old CommonJS-only Node.js | Legacy projects |
| `classic` | TypeScript's pre-Node algorithm | Almost never |
The node16 and nodenext values differ in one way. node16 is frozen to the behavior of Node.js 16. nodenext follows the latest Node.js behavior and can change as Node.js changes. For a new project, nodenext is the usual choice.
How the two options are linked
You rarely set both. TypeScript picks a default moduleResolution from module, and some module values require a matching resolution mode.
| If `module` is | Then `moduleResolution` defaults to |
|---|---|
| `commonjs` | `node10` |
| `node16` | `node16` |
| `nodenext` | `nodenext` |
| `preserve` | `bundler` |
| `es2015` to `esnext` | `classic` (so set it yourself) |
The last row is a trap. If you set module: "esnext" and nothing else, the resolution falls back to a legacy algorithm that does not understand package.json exports. In that case, set moduleResolution explicitly.
TypeScript also enforces some pairings. When module is node16 or nodenext, moduleResolution must be the matching value. Mixing module: "nodenext" with moduleResolution: "bundler" is an error. The reverse direction is looser: moduleResolution: "bundler" works with module set to preserve or any of the ES values, such as es2022 or esnext. It does not work with commonjs on older compiler versions, so check your TypeScript version if you see an error here.
Configuration for Node.js
If Node.js runs your emitted files directly, use nodenext for module. That choice also selects nodenext resolution.
{
"compilerOptions": {
"module": "nodenext",
"target": "es2022",
"strict": true,
"outDir": "dist"
}
}Here module does two jobs. It tells TypeScript to emit ESM or CommonJS depending on each file, and it makes the compiler follow Node.js import rules, including the extension requirements for ES modules. The target option is separate. It controls which JavaScript language features appear in the output, such as classes and async functions, and has nothing to do with the module system.
Configuration for a bundler
If a bundler processes your code, the Node.js rules are stricter than you need. A bundler lets you write extensionless relative imports and reads package.json exports, so the checker should allow both.
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"noEmit": true
}
}Three details matter here.
module: "preserve"tells TypeScript not to rewrite import and export syntax. The bundler does that work.moduleResolution: "bundler"uses resolution rules that match what bundlers do.noEmit: truemeanstsconly type-checks. The bundler produces the real output.
Because preserve already implies bundler resolution, writing moduleResolution out is optional. Many teams write it anyway, since it makes the intent visible in the file.
A worked comparison
Take one import in a file called src/main.ts:
import { greet } from "./greet";The file on disk is src/greet.ts. Whether this compiles depends on the settings.
| Settings | Result |
|---|---|
| `module: "nodenext"`, file is ESM | Error. A relative import in an ES module needs an extension. |
| `module: "nodenext"`, file is CommonJS | Compiles. CommonJS resolution still allows extensionless paths. |
| `module: "preserve"`, `moduleResolution: "bundler"` | Compiles. |
| `module: "commonjs"`, `moduleResolution: "node10"` | Compiles. |
Under nodenext, the same import written for an ES module must read:
// src/main.ts in a package whose package.json has "type": "module"
import { greet } from "./greet.js";
console.log(greet("world"));The specifier says .js even though the source file is greet.ts. TypeScript does not rewrite import paths when it emits, so the path in your source has to be the path that will exist at run time. The compiler maps ./greet.js back to ./greet.ts while checking. A later chapter covers these extension rules in full.
The row for a CommonJS file shows why nodenext is more than a setting about syntax. The same project can hold both kinds of files, and each follows different rules.
Common errors
- Setting only
module: "esnext". Resolution falls back toclassic, and packages that useexportsfail to resolve. SetmoduleResolutionas well, or switch topreserve. - Using
nodenextfor code a bundler handles. You are then forced to write.jsextensions and follow Node.js format rules that your bundler does not need. The code works, but it asks more of you than necessary. - Using
bundlerfor code Node.js runs directly. The checker accepts extensionless imports, but Node.js rejects them at run time. TypeScript reports no error, and the program fails when you run it. - Treating
targetas a module setting.targetpicks the language level. It does not change import syntax in modern setups.
Checking what you have
You can print the final configuration, including defaults the compiler filled in, with a single command:
npx tsc --showConfigIf you did not set moduleResolution, the output shows the value TypeScript derived from module. Reading this is a quick way to confirm that the default you expected is the default you got.
Exercise
A project's tsconfig.json contains only "module": "esnext" and "strict": true. It imports a package whose package.json defines an exports map, and the import fails to resolve. Name the likely cause and give one fix for code run by Node.js after a build, and one fix for code processed by a bundler.
Show answer
With module set to esnext, moduleResolution defaults to classic, which does not read package.json exports. For code that Node.js runs directly, set "module": "nodenext", which also selects nodenext resolution. For code processed by a bundler, set "module": "preserve" (which implies bundler resolution) or keep "module": "esnext" and add "moduleResolution": "bundler".
Exercise
You use "module": "preserve" with "moduleResolution": "bundler" and "noEmit": true. Your script imports "./util" without an extension, tsc reports no errors, and the build is run by Node.js with no bundler. What happens when the program starts, and what configuration change moves the problem to compile time?
Show answer
Node.js fails at run time because ES module relative imports need an explicit extension, and the checker did not warn about it. Switching to "module": "nodenext" makes tsc report the missing extension, so you must write "./util.js".