Module resolution
- TypeScript course
- Expert
- Lesson 15/35
- 4 min
Learn moduleResolution options.
What is moduleResolution?
moduleResolution determines how an import statement finds its target file. Common values: node (classic Node resolution), node16/nodenext (Node 16+ rules), and bundler (for Vite, webpack, and similar).
{
"compilerOptions": {
"moduleResolution": "bundler"
}
}bundler allows extensionless imports; node16 requires full extensions.
node vs node16 vs bundler
node is classic CommonJS resolution and allows extensionless imports. node16/nodenext applies ESM rules and requires the .js extension. bundler mimics bundler behavior and is independent of the module option.
// node16: import "./foo.js" // extension required
// bundler: import "./foo" // extensionless OKA wrong choice can cause runtime module-not-found errors.
Practice Resolve the module path
Write a resolveModule function taking specifier and strategy. For bundler return an extensionless string, for node16 a .js extension string, and for node a classic resolution string. Print the result of resolveModule("./foo", "bundler").
Starter code
// resolveModule: çözümleme stratejisine göre yol döndürür
// birinci strateji için uzantısız
// ikinci strateji için .js uzantılı
// node için klasik
// "./foo" ve "bundler" için sonucu yazdırSample solution
type Resolution = "node" | "node16" | "bundler";
function resolveModule(specifier: string, strategy: Resolution): string {
if (strategy === "bundler") {
return specifier + " (no extension required)";
}
if (strategy === "node16") {
return specifier + ".js (full path required)";
}
return specifier + " (node classic)";
}
console.log(resolveModule("./foo", "bundler"));Expected output
./foo (no extension required)
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which moduleResolution suits a project using Vite or webpack?
- node16
- classic
- bundler Correct answer
- amd
Explanation: bundler is designed for bundler-based projects and allows extensionless imports; node16 requires full extensions, and classic and amd are not suited to modern bundlers.
2 Choose bundler resolution
{
"compilerOptions": {
"moduleResolution": "____"
}
}
{
"compilerOptions": {
"moduleResolution": "bundler"
}
}
Explanation: bundler is the right value for bundler-based projects; node is classic CommonJS, classic is legacy, and amd is loader-focused.
3 Resolution strategies
| Term | Meaning |
|---|---|
node |
classic CommonJS resolution |
node16 |
ESM rules, extension required |
bundler |
bundler behavior |
nodenext |
equivalent to node16 |