Sub topic 5
How TypeScript Resolves Your Package: node10, node16/nodenext and bundler
This chapter assumes you can read tsconfig.json, know what package.json is, and have used import and export in TypeScript. You do not need to know the exports field yet. The next chapters cover it.
Why resolution matters to a library author
When a consumer writes import { slugify } from "@acme/slugify", TypeScript must decide which .d.ts file describes that import. The process is called module resolution. The moduleResolution setting in the consumer's tsconfig.json controls it. You do not control that setting. You can only make your package work under the settings consumers are likely to use.
Resolution also applies to your own code. When you write import { slugify } from "./slugify.js" inside your library, the compiler uses your moduleResolution setting to find the file.
There are three modes you will meet in practice.
| Mode | Typical use | Reads `exports` | Requires file extensions on relative imports |
|---|---|---|---|
| `node10` (also called `node`) | Old projects, legacy configs | No | No |
| `node16` / `nodenext` | Code that runs directly in Node.js | Yes | Yes, for ESM files |
| `bundler` | Code processed by Vite, webpack, esbuild and similar tools | Yes | No |
TypeScript 5.x also accepts classic, but it is obsolete and you can ignore it.
node10: the legacy algorithm
The node10 mode copies the lookup rules of Node.js version 10 and earlier. It knows nothing about exports. For a bare specifier such as @acme/slugify, it looks in node_modules/@acme/slugify and reads package.json in this order:
- The
typesfield (or its aliastypings). - The
mainfield, replacing the extension with.d.ts. - An
index.d.tsfile at the package root.
It also does not understand subpaths in the modern sense. The import @acme/slugify/locale is treated as a plain file path: it looks for node_modules/@acme/slugify/locale.d.ts or locale/index.d.ts. If your declaration files live in dist/locale/index.d.ts, node10 consumers cannot find them through the exports map. This is why the legacy fields from the example package.json (main and types) remain useful as fallbacks.
Many projects still use node10, so a published library should keep working for them. Packages that rely on subpath exports need extra care. A common fix is a small stub directory at the package root, such as locale/package.json with a types field. Whether that is worth the effort depends on your audience.
node16 and nodenext: Node.js rules
The node16 and nodenext modes model what Node.js actually does at run time. They differ in one way: node16 is frozen at the behavior of Node 16, while nodenext follows the newest Node.js behavior as TypeScript updates. For new projects, prefer nodenext. Both require the module setting to match, which is why the example tsconfig.json sets both to NodeNext.
These modes bring three important rules.
Each file is either ESM or CommonJS
TypeScript decides the format of every .ts file the way Node.js does:
- A
.mtsfile is ESM, and a.ctsfile is CommonJS. - A
.tsfile takes its format from the nearestpackage.json. If it has"type": "module", the file is ESM. Otherwise it is CommonJS.
The same rule applies to .js and .d.ts files. This is the reason the type field matters so much for libraries, a topic the previous chapter covered.
Relative imports in ESM need extensions
In an ESM file, Node.js does not guess extensions. So TypeScript requires you to write them. You write the extension of the output file, not the source file:
// src/index.ts, in a package with "type": "module"
export { slugify } from "./slugify.js"; // correct
export { slugify } from "./slugify"; // error TS2835 under nodenext
export { slugify } from "./slugify.ts"; // error unless special options are setThe .js extension looks odd because the file on disk is slugify.ts. TypeScript maps ./slugify.js to ./slugify.ts during resolution. After compilation, the emitted file really is slugify.js, so the import works in Node.js.
The exports field is honored
When TypeScript sees a bare specifier, it reads the exports field of the target package and picks a path using conditions. The conditions that TypeScript considers depend on the importing file:
- An
importstatement in an ESM file activates theimportcondition. - A
require()call, or animportin a CommonJS file, activates therequirecondition. - TypeScript always activates
typesanddefault, andnodefor these modes.
A key detail is that the importing file's format selects the condition, not the consumer's preference. This is how a single package can serve both formats, and the dual-package chapter returns to it.
If exports is present and the requested path is not listed, resolution fails. It does not fall back to main or to the file system. This is the encapsulation that subpath exports rely on.
bundler: the compromise mode
The bundler mode was added in TypeScript 5.0. It models tools such as Vite, esbuild and webpack. Those tools read exports and conditions like Node.js, but they allow extensionless relative imports and directory imports. It also does not enforce the ESM and CommonJS split per file. In bundler mode, an import statement always activates the import condition.
A project using bundler must set module to ESNext, ES2020, ES2022 or Preserve. It cannot use module: "NodeNext".
The practical consequence for library authors is that bundler is more forgiving than nodenext. Code that passes under bundler can still fail under nodenext. A typical failure is a relative import without an extension inside your published .d.ts files. Such an import works for bundler users and breaks for Node.js users.
Choosing a setting for your own library
Pick the strictest realistic setting for compiling your library, so you catch problems before consumers do. For a library published to run in Node.js, or in both Node.js and bundlers, use:
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
}If you set only module to NodeNext, current TypeScript versions infer moduleResolution as NodeNext too. Writing both makes the intent clear.
Do not compile a library with bundler unless you also ship bundled output. Declaration files emitted from extensionless imports are not valid for Node.js consumers.
The setting you choose for your own build is separate from the settings your consumers use. You must reason about both.
Common errors
The following table lists symptoms you will see and what usually causes them.
| Symptom | Likely cause |
|---|---|
| `TS2835: Relative import paths need explicit file extensions` | An ESM file under `node16` or `nodenext` imports `./file` without `.js` |
| `TS2307: Cannot find module '@acme/slugify/locale'` under `node10` | Subpath export with no `node10` fallback |
| `TS7016: Could not find a declaration file` | The `types` condition or field points to a missing file, or the file is not in the published package |
| `TS1479: The current file is a CommonJS module whose imports will produce require calls` | A CommonJS consumer imports an ESM-only package under `nodenext` (in older TypeScript versions) |
| Types resolve in the editor but fail in `tsc` | Editor and build use different `tsconfig.json` files |
Note the TS1479 case. TypeScript 5.x with newer Node.js versions allows require() of ESM in some cases, so the exact outcome depends on your versions. Test with the versions your consumers use.
Testing resolution yourself
You can ask the compiler how it resolves a path. The --traceResolution flag prints every step. Run it in a test consumer project, not in the library itself:
npx tsc --noEmit --traceResolution | grep "@acme/slugify"Change moduleResolution in the consumer's tsconfig.json between node10, nodenext and bundler, then compare the output. You will see the exports field read in one mode and ignored in another. Newer TypeScript versions may warn that node10 is deprecated, so you may need to add "ignoreDeprecations" for it.
Summary of decisions
- Compile your library with
moduleandmoduleResolutionset toNodeNext. - Write
.jsextensions on relative imports in ESM source. - Keep
mainandtypesinpackage.jsonsonode10consumers still find your declarations. - Use
exportsfor everything else, and treat unlisted paths as private. - Test with at least
node10,nodenextandbundlerconsumers before release.
Exercise
A consumer uses moduleResolution: "node10" and imports @acme/slugify/locale. Your package lists ./locale in exports, with files under dist/locale/. The import fails with TS2307. Why does it fail, and what is one way to fix it?
Show answer
The node10 mode ignores the exports field, so it looks for node_modules/@acme/slugify/locale.d.ts or locale/index.d.ts at the package root. Those files do not exist, because the declarations are in dist/locale/. One fix is to add a locale/package.json stub at the package root whose types field points to ../dist/locale/index.d.ts, and to include that directory in files. Another is to document that subpath imports require node16, nodenext or bundler.
Exercise
Your library source contains import { helper } from "./helper"; in a package with "type": "module", compiled with module: "NodeNext". What error do you get, and how do you fix it?
Show answer
TypeScript reports TS2835 because ESM relative imports need explicit extensions under nodenext. Change the import to ./helper.js, which is the name of the emitted file, even though the source file is helper.ts.