← Back to TypeScript Course | Chapter 17: TypeScript Configuration | Lesson 3 of 7

Module Resolution की जानकारी

Module resolution वह process है जो TypeScript उस file ढूंढने के लिए उपयोग करता है जिसकी ओर एक import refer करता है। module और moduleResolution settings यह तय करती हैं कि अलग-अलग JavaScript environments के लिए imports कैसे interpret होते हैं।
Syntax
typescript
{
  "compilerOptions": {
    "moduleResolution": "node",
    "baseUrl": "./src"
  }
}

Module Resolution क्या करता है

जब TypeScript एक import statement देखता है, इसे असली file या package जिसे reference किया गया उसे locate करना होता है — module resolution rules का वह set है जो exactly यह तय करता है कि यह कैसे और कहां देखता है।

उदाहरण: What Module Resolution Does

typescript
// 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

typescript
// 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

typescript
// 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

typescript
// 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

typescript
// tsc --traceResolution
console.log("traceResolution prints exactly which paths were tried and why");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. moduleResolution: "node10" modern ESM packages के साथ उपयोग करना जो exports पर depend करते हैं, इसलिए उनके types नहीं मिल सकते।
  2. node16/nodenext ESM के under relative imports में file extension छोड़ना, जहां "./utils.js" required है।
  3. एक 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.