Sub topic 1
What Module Resolution Does
Module resolution is the process TypeScript uses to turn an import specifier into a real module file. An import specifier is the text inside an import or require statement, such as ./format or date-fns.
For example:
import { formatDate } from "./format";
import { addDays } from "date-fns";TypeScript must answer two questions:
- Which file does
./formatrefer to? - Which file inside the installed
date-fnspackage should be used?
The compiler searches according to your project settings and the rules of the selected resolution mode. It may inspect file extensions, folders, package.json fields, package export maps, and declaration files.
Prerequisites
This chapter assumes that you can write TypeScript import and export statements, run tsc, and read a basic tsconfig.json. You should also know that Node.js supports both CommonJS and ES modules.
Resolution is different from output
Module resolution answers where an imported module comes from. Module output answers what JavaScript format TypeScript should produce.
These settings are related, but they are not the same:
| Concern | Question | Common setting |
|---|---|---|
| Module resolution | Which file does an import refer to? | `moduleResolution` |
| Module output | What JavaScript module format is emitted? | `module` |
| JavaScript syntax level | Which language features are emitted? | `target` |
For instance, moduleResolution might select src/format.ts, while module determines whether the generated file uses import statements or CommonJS calls such as require().
A project can type-check successfully while still producing runtime errors if its compiler settings do not match the runtime or bundler that will execute the output. TypeScript checks that an import can be understood. It does not automatically make every runtime import valid.
What TypeScript searches
For a relative specifier such as ./format, TypeScript starts from the importing file's directory. It then checks candidate files and folders according to the resolution mode. A candidate might be:
- A TypeScript source file such as
format.ts - A JavaScript file such as
format.js - A declaration file such as
format.d.ts - An
indexfile inside aformatdirectory - A path selected by a package's
exportsfield
For a bare specifier such as date-fns, TypeScript searches package locations, normally beginning with node_modules. It reads package metadata to find the package's entry point and type information.
The exact candidates and their order depend on moduleResolution. The older node modes, nodenext, and bundler model different environments. Later chapters examine those differences.
The runtime must agree
Suppose main.ts imports ./format. TypeScript may find format.ts during type-checking. After compilation, the runtime may look for format.js, depending on the module format and runtime rules.
That is why an import can have no TypeScript error but fail when the program runs. The compiler's lookup and the runtime's lookup must describe the same project layout.
A reliable configuration matches the tool that will run the code:
- Use Node-oriented settings when Node.js loads the emitted files directly.
- Use bundler-oriented settings when a bundler rewrites and packages imports.
- Use compatible settings for published libraries so consumers can resolve both the code and its declarations.
A small example
Assume this project contains:
src/
main.ts
format.tsThe import in main.ts describes a relationship, not a literal filename:
import { format } from "./format";
console.log(format("ready"));Resolution decides whether that relationship points to format.ts, another supported file, or no file at all. If the compiler cannot find a valid target, it reports a module resolution error. If it finds the wrong target, the code may type-check against an unexpected API.
When an import fails, first identify the importing file, the exact specifier, the selected moduleResolution mode, and the files that actually exist. Those four facts usually narrow the problem quickly.
Exercise
Does module resolution decide whether TypeScript emits ES module syntax or CommonJS syntax?
Show answer
No. Module resolution finds the file represented by an import. The module setting controls the JavaScript module format that TypeScript emits.