Module Resolution
Module resolution is the algorithm TypeScript uses to turn an import specifier into a file on disk (and into that file's types). It is controlled by moduleResolution and must match how your runtime or bundler resolves the same specifier.
Detailed Explanation
import { start } from "./app" looks simple. The compiler has to decide whether that is app.ts, app.tsx, app.d.ts, app/index.ts, or a .js file with a sibling .d.ts. import fs from "fs" has to decide between a built-in, a node_modules package, and that package's types / exports map. Module resolution is that decision.
The moduleResolution option picks the algorithm:
node10(historicallynode) — classic Node CommonJS lookup:package.json"main"and"types", index files, implicit extensions.nodenext/node16— Node's current ESM rules: theexportsfield, explicit extensions in relative ESM imports, and different rules forimportvsrequire.bundler— a looser lookup for tools like Vite and webpack that accept extensionless relative imports and packageexports.
module (what you emit) and moduleResolution (how you look up) are a pair. module: "NodeNext" wants moduleResolution: "NodeNext". A mismatch is how you get a program that type-checks and then fails in Node, or the reverse.
Resolution also consults baseUrl, paths, rootDirs, typeRoots, and package.json "imports" / "exports". paths is a compile-time remap ("@app/*": ["src/*"]). It does not rewrite emit unless you add a bundler or a tool like tsc-alias. Forgetting that is the usual "works in the editor, cannot find module at runtime" bug.
For a package, TypeScript looks at exports conditions (types, import, require), then types / typings, then main. A package that ships only ESM should say so in exports. A package that ships CJS and ESM needs a types condition that points at the right .d.ts for each, or you get import-equals-require mismatches.
nodenext is strict about extensions. import { x } from "./foo" is an error in an ESM file; you write ./foo.js even when the source is foo.ts. The compiler maps foo.js to foo.ts for type-checking. That looks wrong the first time and is the Node rule.
When an import cannot be resolved, the error is Cannot find module '…' or its corresponding type declarations. That sentence has two halves: the file is missing, or the file is JavaScript without types. allowJs plus a .d.ts, or an @types package, or declare module fills the second half.
tsc --traceResolution prints the lookup. Use it before changing paths at random.
Code Examples
1. Node ESM today.
// 1. Node ESM today.
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true
}
}2. Under NodeNext ESM, relative imports need a JS extension.
// 2. Under NodeNext ESM, relative imports need a JS extension.
import { parseId } from "./ids.js"; // resolves to ids.ts at type-check time
export function load(id: string): number {
return parseId(id);
}3. Compile-time path aliases — the bundler must implement the same map.
// 3. Compile-time path aliases — the bundler must implement the same map.
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@app/*": ["src/*"]
},
"moduleResolution": "bundler"
}
}Common Gotchas
`paths` do not change emitted import specifiers
Node will not honor `@app/foo` unless a bundler or a rewrite step exists. Aliases are a type-checker (and bundler) feature.
`module` and `moduleResolution` should match
`ESNext` plus `node10` will accept imports Node ESM will reject, or the reverse.
`./foo.js` importing `foo.ts` is intentional under NodeNext
write the specifier the runtime will see.
A package with broken `exports.types` type-checks against the wrong file
`traceResolution` shows which `.d.ts` won. `skipLibCheck` will not fix a wrong entry.