Sub topic 5
How TypeScript Resolves Your Package: `node10`, `node16`/`nodenext`, and `bundler`
Assume you know TypeScript imports, exports, npm, and basic tsconfig.json settings. This chapter explains how the moduleResolution setting finds the JavaScript and declaration files in an installed package.
Why module resolution matters
Module resolution is TypeScript’s process for turning an import such as @acme/slugify into a file on disk. TypeScript must find both:
- The JavaScript module that will run.
- The declaration file that describes its types.
For a package with this configuration:
{
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
}the resolver must understand the exports field and the types condition. Different resolution modes understand different package rules.
`node10`: the legacy resolver
node10, formerly called node, models older Node.js behavior. It commonly checks these package fields:
typesortypingsfor declarations.mainfor JavaScript.- An
index.d.tsorindex.jsfile when needed.
It does not model modern exports and imports behavior. A package may work in a node10 project because it includes main and types, while a modern Node application uses a different entry point through exports.
Use this mode mainly for older projects that cannot yet move to modern Node resolution.
`node16` and `nodenext`
These modes model Node’s modern ESM and CommonJS rules. They inspect the package’s "type" field and file extensions such as .mjs, .cjs, .mts, and .cts.
They also understand:
- The
exportsfield. - Conditional exports such as
import,require, andtypes. - Package self-references.
- The requirement for file extensions in many relative ESM imports.
node16 models Node 16’s behavior. nodenext follows the current Node behavior supported by the installed TypeScript version. Use the mode that matches the runtime you support, and pair it with a compatible module setting.
For an ESM package, a relative import may need its output extension:
import { slugify } from "./slugify.js";
export { slugify };The source file can be TypeScript, but .js tells Node which emitted file the runtime should load.
`bundler`: package rules without Node’s runtime checks
The bundler mode understands modern package exports and conditions. It is designed for applications whose bundler, such as Vite, esbuild, or webpack, resolves modules before they reach a runtime.
Unlike node16 and nodenext, it generally does not require extensions on relative imports. This is convenient:
import { slugify } from "./slugify";The bundler can later resolve the emitted module. However, the code may fail when run directly by Node if Node requires an explicit extension.
Choose bundler when a bundler controls execution. Choose node16 or nodenext when Node itself runs the emitted files.
Comparing the modes
| Mode | Understands `exports` | Requires Node-style extensions | Typical use |
|---|---|---|---|
| `node10` | No | Legacy behavior | Older Node projects |
| `node16`/`nodenext` | Yes | Often for ESM relatives | Direct Node applications |
| `bundler` | Yes | Usually no | Bundled applications |
A library should test its package with the resolution modes used by its consumers. A package that passes only under bundler might still fail for a consumer running Node directly.
A practical configuration
A modern Node consumer might use:
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true
}
}A bundled application might use:
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true
}
}Test both configurations when your library claims to support both environments. Errors such as “cannot find module” or “no declaration file” often indicate that the package’s exports map, file extensions, or declaration paths do not match the consumer’s resolver.
Exercise
Which resolution mode should you usually choose for TypeScript code whose emitted JavaScript runs directly in modern Node, and why?
Show answer
Choose node16 or nodenext because these modes model Node’s ESM and CommonJS rules, including package exports, conditions, and required relative-file extensions.