Sub topic 11
Tracing Resolution with `traceResolution` and `explainFiles`
Assume you can write TypeScript imports and run tsc. This chapter focuses on diagnosing a project where an import fails, resolves to the wrong file, or receives unexpected types.
TypeScript performs module resolution when it turns an import such as "./greet.js" or "lodash" into a real file. The result depends on your moduleResolution, the importing file's module format, file extensions, and package metadata.
Start with the failing import
Suppose this file contains an import that does not compile:
// src/main.ts
import { greet } from "./greet.js";
console.log(greet("world"));The source file might be src/greet.ts. Under nodenext, TypeScript can resolve the .js specifier to the TypeScript source file during type-checking. If the import uses "./greet" instead, resolution may fail because Node.js ES modules require an explicit extension at runtime.
Do not guess which rule caused the failure. Ask the compiler to show its work.
Use `traceResolution`
Run this command from the directory containing tsconfig.json:
npx tsc --noEmit --traceResolution--noEmitprevents JavaScript output.--traceResolutionprints the files and package fields TypeScript checks.npx tscuses the TypeScript version installed in the project.
The output can be long. Save it to a file and search for the import:
npx tsc --noEmit --traceResolution > resolution.log 2>&1
grep -n "greet" resolution.logA trace usually contains messages like these:
| Trace message | Meaning |
|---|---|
| `Resolving module './greet.js'` | TypeScript started with this import specifier. |
| `Loading module as file` | It is checking direct file-name candidates. |
| `File ... exists` | A candidate was found. |
| `Directory ... does not exist` | TypeScript rejected that directory candidate. |
| `Package ...` | It is reading a package from `node_modules`. |
| `======== Module name ... was successfully resolved` | The final resolution succeeded. |
Read the trace from the first Resolving module line down to the success or failure line. The first rejected candidate is not always the cause. TypeScript often tries several valid alternatives before choosing one.
Understand the search path
For a relative import, TypeScript starts from the importing file's directory. For a bare package import such as "example-lib", it searches package locations such as node_modules.
The active resolution mode changes the candidates. In nodenext, TypeScript considers the importing file's ESM or CommonJS format and follows Node.js package rules. It may inspect package.json, exports, and conditions such as import, require, and types.
A useful debugging question is:
- What exact specifier did the source file use?
- Which file format does TypeScript assign to the importing file?
- Which
moduleResolutionsetting is active? - Which candidate did the trace accept?
- Does the emitted import match that candidate's runtime path?
Use `explainFiles`
--explainFiles answers a different question: why is each file part of the TypeScript program?
npx tsc --noEmit --explainFilesThis output can show that a file was included because:
- It is matched by the
includepattern. - It is imported by another source file.
- It is included through a referenced project.
- It is a library declaration file required by the selected
targetorlib.
Use explainFiles when the problem is “Why is this file being checked?” Use traceResolution when the problem is “Which file does this import mean?”
A practical debugging sequence
First reproduce the error with the project's normal tsconfig.json. Passing a different project with -p can produce a completely different trace.
Exercise
Which flag should you use to find out why src/legacy.ts is included in the TypeScript program: --traceResolution or --explainFiles?
Show answer
Use --explainFiles. It reports why each file belongs to the program. Use --traceResolution to investigate which file an import specifier resolves to.