Module Resolution की जानकारी
In this page:
{
"compilerOptions": {
"moduleResolution": "node",
"baseUrl": "./src"
}
}
Module Resolution क्या करता है
जब TypeScript एक import statement देखता है, इसे असली file या package जिसे reference किया गया उसे locate करना होता है — module resolution rules का वह set है जो exactly यह तय करता है कि यह कैसे और कहां देखता है।
उदाहरण: What Module Resolution Does
// import { helper } from "./utils";
// TypeScript must locate the actual utils.ts/.d.ts file this refers to.
console.log("Module resolution determines how imports map to real files");
Node10 Resolution
Node10 resolution traditional Node.js package और file lookup behavior follow करता है, और आमतौर पर पुराने CommonJS-style projects से जुड़ा होता है जो package.json exports field से पहले के हैं।
उदाहरण: Node10 Resolution
// tsconfig.json
// { "compilerOptions": { "moduleResolution": "node10" } }
console.log("Node10 follows traditional CommonJS-style lookup rules");
Node16 और NodeNext
Node16 और NodeNext modern Node.js module behavior को ज़्यादा precisely model करते हैं, package.json metadata और CommonJS और ES modules के बीच असली distinction शामिल करते हुए, जो पुराने resolution modes नहीं समझते।
उदाहरण: Node16 and NodeNext
// tsconfig.json
// { "compilerOptions": { "moduleResolution": "nodenext" } }
console.log("NodeNext models modern Node's package.json exports and ESM/CJS split");
Bundler Resolution
Bundler resolution उन projects के लिए design किया गया है जहां webpack या Vite जैसा एक tool module loading handle करता है — यह Node के runtime resolution rules apply होने की उम्मीद किए बिना package exports और bundler-specific conventions support करता है।
उदाहरण: Bundler Resolution
// tsconfig.json
// { "compilerOptions": { "moduleResolution": "bundler" } }
console.log("Bundler resolution suits projects using webpack/Vite for loading");
Resolution Trace करना
traceResolution compiler flag exactly वे paths print कर सकता है जो TypeScript ने try किए और एक module lookup क्यों succeed या fail हुई, जो एक mysterious "cannot find module" error debug करते समय invaluable है।
उदाहरण: Tracing Resolution
// tsc --traceResolution
console.log("traceResolution prints exactly which paths were tried and why");
moduleResolution: "node10"modern ESM packages के साथ उपयोग करना जोexportsपर depend करते हैं, इसलिए उनके types नहीं मिल सकते।node16/nodenextESM के under relative imports में file extension छोड़ना, जहां"./utils.js"required है।- एक resolution mode चुनना जो code असल में कैसे चलता है उससे match न करे (Node बनाम एक bundler), इसलिए imports compile होती हैं लेकिन runtime पर fail होती हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: