Lesson 5 of 9 · 27m
Module Resolution: `node16`, `nodenext`, `bundler`, and Reality
Trace exactly how TypeScript chooses files for import specifiers and why the same import can typecheck in one mode and fail in another.
Module resolution is where TypeScript stops being a type system and starts negotiating with twenty years of JavaScript packaging history. Teams hit this as an import that works in Vite, fails in Node, resolves in Jest, and produces three different errors depending on which tsconfig the editor opened. The fix is not guessing flags. The fix is understanding the lookup algorithm your runtime actually uses.
moduleResolution is a runtime compatibility choice
The common options encode different worlds:
node10/node: legacy CommonJS resolution node16/nodenext: Node ESM/CJS-aware resolution
bundler: bundler-style resolution classic: historical, avoidChoose according to the consumer. A library shipped to modern Node should usually use module: "NodeNext" and moduleResolution: "NodeNext". An application bundled by Vite, esbuild, webpack, or Rollup often uses moduleResolution: "Bundler" because the bundler owns final resolution.
Extension behavior is not cosmetic
In Node ESM, relative imports require explicit file extensions in emitted JavaScript:
import { parseConfig } from "./config.js"The source file may be config.ts, but TypeScript understands the .js specifier as referring to the emitted file. This feels backward the first week and becomes natural once you accept the rule: imports describe runtime artifacts, not authoring conveniences.
With bundler resolution, extensionless imports are commonly accepted because the bundler resolves them. That convenience is one reason application code cannot always be copied directly into a Node-native library package.
Trace before changing flags
TypeScript has a real diagnostic. Use it:
tsc --traceResolution --noEmit > trace.txtSearch for the exact specifier:
Resolving module '@acme/core' from '/repo/apps/web/src/main.ts'
Loading module '@acme/core' from 'node_modules'; package.json existsThe trace tells you which package.json fields were considered, which candidate files were tried, and where resolution stopped. Ten minutes of trace reading beats an afternoon of flag roulette.
Conditional exports change the answer per environment
A package can expose different files depending on import conditions:
{"exports": {".": {"types": "./dist/index.d.ts", "import": "./dist/index.mjs", "require": "./dist/index.cjs"}}}TypeScript, Node, bundlers, and test runners may activate different conditions. That is not a TypeScript bug. It is the package contract being interpreted in multiple environments. Your job is to decide which environments are supported and test them explicitly.
Field notes
moduleResolutionandmoduleare coupled; changing one without understanding the other creates bizarre diagnostics.allowImportingTsExtensionsis for no-emit or rewriting pipelines, not general Node output.- TypeScript does not rewrite import specifiers in emitted JavaScript.
resolveJsonModulechanges type shape and emit behavior; test it before enabling it in shared configs.- When in doubt, make the runtime command part of CI. Typecheck success is not execution success.
Was this lesson helpful?