Program construction and resolution
Concept and boundary
Program construction includes root files, reachable imports and selected libraries; resolution uses compiler options and host file access. resolveModuleName exposes one lookup result, not a complete runtime resolver. Cache validity depends on project inputs.
Pinned implementation breadcrumbs
In the installed TypeScript 5.8.3 bundle lib/typescript.js the following function definitions were located: createProgram (line 125891); resolveModuleName (line 44677). These are navigation anchors for source reading, not public API contracts. The probes below observe public behavior; they do not prove every internal step.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: resolve-module-name
Revisit reference fixture ts-api-resolve-module-name (historical V27, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");fs.writeFileSync("main.ts", ""); fs.writeFileSync("dep.ts", "export const item=7;");const file=path.resolve("main.ts"), options={moduleResolution:ts.ModuleResolutionKind.Node10};const found=ts.resolveModuleName("./dep",file,options,ts.sys).resolvedModule;assert(found); assert.equal(path.resolve(found.resolvedFileName),path.resolve("dep.ts"));assert.equal(ts.resolveModuleName("./missing",file,options,ts.sys).resolvedModule,undefined);console.log("./dep -> dep.ts; ./missing -> unresolved");Expected stdout (exit 0):
./dep -> dep.ts; ./missing -> unresolvedWorkshop 2: create-program
Revisit reference fixture ts-api-create-program (historical V23, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS };const program = ts.createProgram(["input.ts"], options);const roots = program.getRootFileNames();assert.deepEqual(roots, ["input.ts"]);assert(program.getSourceFiles().some(f => f.fileName.endsWith("dep.ts")));assert.equal(ts.getPreEmitDiagnostics(program).length, 0);console.log(roots.length, "dependency loaded");Expected stdout (exit 0):
1 dependency loadedPractice and recap
Trace a missing relative import, create the dependency and rebuild with a fresh cache.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Program construction and resolution — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Program construction and resolution?
What does workshop 1 (resolve-module-name) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");fs.writeFileSync("main.ts", ""); fs.writeFileSync("dep.ts", "export const item=7;");const file=path.resolve("main.ts"), options={moduleResolution:ts.ModuleResolutionKind.Node10};const found=ts.resolveModuleName("./dep",file,options,ts.sys).resolvedModule;assert(found); assert.equal(path.resolve(found.resolvedFileName),path.resolve("dep.ts"));assert.equal(ts.resolveModuleName("./missing",file,options,ts.sys).resolvedModule,undefined);console.log("./dep -> dep.ts; ./missing -> unresolved");What does workshop 2 (create-program) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS };const program = ts.createProgram(["input.ts"], options);const roots = program.getRootFileNames();assert.deepEqual(roots, ["input.ts"]);assert(program.getSourceFiles().some(f => f.fileName.endsWith("dep.ts")));assert.equal(ts.getPreEmitDiagnostics(program).length, 0);console.log(roots.length, "dependency loaded");