Thuta Learning
TypeScript
IntermediateProgrammingအလယ်အလတ်

ESM Modules & Package Boundaries

TypeScriptသင်ခန်းစာ 1430 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • File တစ်ခုကနေ function, type နဲ့ value တွေကို export လုပ်ပြီး တခြား file မှာ named import နဲ့ ယူသုံးနိုင်ဖို့
  • Source file က .ts ဖြစ်ပေမယ့် NodeNext project ရဲ့ relative import က .js နဲ့ ဘာကြောင့် ဆုံးရလဲ ရှင်းပြနိုင်ဖို့
  • import type ကို သုံးပြီး type-only import တွေကို emit လုပ်တဲ့ JavaScript ထဲက ဖယ်ထုတ်နိုင်ဖို့
  • package.json ရဲ့ "type" နဲ့ "exports" က package ရဲ့ module format နဲ့ public surface ကို ဘယ်လို သတ်မှတ်လဲ ရှင်းပြနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Module ကို ရိုးရိုးလေး ရှင်းရရင်

Module ဆိုတာ ကိုယ့် variable တွေကို ကိုယ်ပိုင်အဖြစ် သိမ်းထားပြီး ဘာကို မျှဝေမလဲ ကိုယ်တိုင် ရွေးတဲ့ file တစ်ခုပါပဲ။ export လို့ မှတ်ထားတဲ့ အရာမှန်သမျှကို တခြား file တွေက သုံးလို့ ရပြီး ကျန်တာအားလုံးကတော့ အထဲမှာပဲ ဝှက်ထားပါတယ်။ တခြား file က လိုတာကို import နဲ့ ယူပါတယ်။

File တစ်ခုစီကို ရှေ့မှာ ကောင်တာပါတဲ့ ဆိုင်လေးတစ်ဆိုင်လို မြင်ကြည့်ပါ။ export က ပစ္စည်းတစ်ခုကို ကောင်တာပေါ် တင်လိုက်တာ ဖြစ်ပြီး import က customer က အဲဒီ ပစ္စည်းကို နာမည်နဲ့ တောင်းတာပါ။ ဒီနည်းနဲ့ program ကြီးကြီးကို အလုပ်တစ်ခုစီ လုပ်တဲ့ file သေးသေးလေးတွေအဖြစ် ခွဲလို့ရပြီး နာမည်တိုင်းက shared space ကြီးတစ်ခုထဲ ပေါက်ထွက်မသွားပါဘူး။

Problem → Why → Solution

ပြဿနာ: editor မှာ ကောင်းနေပြီး Node မှာ crash ဖြစ်တဲ့ import

Helper အားလုံးက စာကြောင်း ၃၀၀၀ ရှိတဲ့ utils.ts တစ်ခုတည်းထဲမှာ ရှိနေတဲ့ project တစ်ခုကို မြင်ကြည့်ပါ၊ ဒါမှမဟုတ် ပိုဆိုးတာက script တွေက global variable တွေကနေ ဆက်သွယ်နေတာပါ။ File နှစ်ခုက format ဆိုတဲ့ function ကို ကိုယ်စီ သတ်မှတ်ထားရင် နောက်ဆုံး load ဖြစ်တဲ့ တစ်ခုက နိုင်သွားပါတယ်။

အဲဒါကို file တွေအဖြစ် ခွဲလိုက်တော့ နောက်ထပ် အံ့သြစရာ ကြုံရပါတယ်: path မှာ file extension မပါတာကြောင့် Node.js က ERR_MODULE_NOT_FOUND နဲ့ crash ဖြစ်သွားပါတယ်။

src/app.ts
// package.json has "type": "module"
import { add } from "./math"; // no extension

console.log(add(2, 3));

// With "module": "NodeNext", tsc reports error TS2835 here.
// With older settings it compiles, then at runtime:
//   node dist/app.js
//   Error [ERR_MODULE_NOT_FOUND]: Cannot find module '.../dist/math'

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

Node ရဲ့ ES module loader က အရင် CommonJS require() လို file extension ကို မှန်းမပေးပါဘူး၊ ရေးထားတဲ့ path အတိအကျကိုပဲ load လုပ်ပါတယ်။ TypeScript ကလည်း JavaScript emit လုပ်တဲ့အခါ import path တွေကို ပြန်ရေးမပေးပါဘူး။ ဒါကြောင့် .ts source ထဲက path က emit လုပ်ပြီးသား .js file အတွက် မှန်တဲ့ path ဖြစ်ရပါမယ်။

အဓိက concept: export, import နဲ့ runtime path

ES modules (ESM) က JavaScript ရဲ့ standard module system ဖြစ်ပြီး TypeScript ကလည်း syntax တူတူကို သုံးပါတယ်။ File တစ်ခုမှာ top-level import ဒါမှမဟုတ် export တစ်ခု ပါလာတာနဲ့ module ဖြစ်သွားပါတယ်။ Module ထဲမှာ top-level နာမည်တွေက default အားဖြင့် private ဖြစ်ပါတယ်။ export က နာမည်တစ်ခုကို မြင်နိုင်အောင် လုပ်ပေးပြီး import က ယူလာပေးပါတယ်။ Function, class, constant တွေအပြင် type နဲ့ interface တွေကိုလည်း export လုပ်လို့ ရပါတယ်။

Export ပုံစံ နှစ်မျိုး ရှိပါတယ်။ Named export (export function add) ကို curly brace နဲ့ နာမည်အတိအကျ ရေးပြီး import လုပ်ရပါတယ်: import { add } from "./math.js"။ Default export (export default function) ကိုတော့ brace မပါဘဲ ကြိုက်တဲ့ နာမည်နဲ့ import လုပ်လို့ ရပါတယ်။ Named export တွေက ရှာရလွယ်ပြီး rename ကို လုံလုံခြုံခြုံ လုပ်လို့ရတာကြောင့် team အများစုက ပိုကြိုက်ကြပါတယ်။

text
Source files, compiled files and the import path
------------------------------------------------
src/math.ts                        src/app.ts
-----------                        ----------
export function add(...)  <------  import { add } from "./math.js";
const SECRET = 42;  (not exported: app.ts cannot see it)

        | tsc compiles each file, import strings stay as written
        v

dist/math.js  <------------------  dist/app.js loads "./math.js"

အပေါ်က diagram က .js extension ကို ရှင်းပြပါတယ်။ TypeScript က math.ts ကို math.js အဖြစ် compile လုပ်ပေမယ့် import string ကိုတော့ မထိပါဘူး။ Runtime မှာ app.js က တကယ်ရှိတဲ့ file ဖြစ်တဲ့ math.js ကို ညွှန်ရပါမယ်။ ဒါကြောင့် module နဲ့ moduleResolution ကို NodeNext ထားရင် output file ရဲ့ extension ကို ရေးရပြီး type-check လုပ်တဲ့အခါ ./math.js က ./math.ts ကို ဆိုလိုတယ်ဆိုတာ TypeScript က နားလည်ပါတယ်။ Vite လို bundler နဲ့ build လုပ်တဲ့ project တွေကတော့ moduleResolution: "bundler" ကို သုံးနိုင်ပြီး bundler က resolve လုပ်ပေးတာကြောင့် extension မပါတဲ့ import တွေကို ခွင့်ပြုပါတယ်။

Type တွေက runtime မှာ ပျောက်သွားတာကြောင့် type တွေကိုပဲ ယူတဲ့ import တစ်ခုမှာ load လုပ်စရာ ဘာမှ မရှိပါဘူး။ import type { User } from "./user.js" လို့ ရေးရင် ဒါကို ရှင်းရှင်းလင်းလင်း ပြောလိုက်တာ ဖြစ်ပြီး line တစ်ခုလုံး output ထဲက ပျောက်သွားပါတယ်။ verbatimModuleSyntax option ဖွင့်ထားရင် TypeScript က ဒီ marker ကို မဖြစ်မနေ တောင်းပါတယ်၊ ဒါမှ ကျန်တဲ့ import တိုင်းက runtime import အစစ် ဖြစ်မှာပါ။ နောက်ဆုံးအနေနဲ့ .js file တစ်ခုက ESM လား CommonJS လားဆိုတာကို Node က အနီးဆုံး package.json ကနေ ဆုံးဖြတ်ပါတယ်: "type": "module" ဆိုရင် ESM ပါ။

မှတ်ထားရမယ့် ပုံစံ
  1. File တိုင်းက ကိုယ်ပိုင် scope
  2. export က နာမည်ကို ကောင်တာပေါ် တင်
  3. import က emit ပြီးသား path ကို ညွှန်
  4. import type က runtime မှာ ပျောက်

Import path ကို TypeScript မပြောင်းပေးပါဘူး — runtime မှာ မှန်ရမယ့် path ကို ရေးပါ။

အထွေထွေ ပုံစံ

typescript
// lib.ts: share names with export
export function name(param: Type): Result { /* ... */ }
export type Shape = { field: Type };
export default function main() { /* ... */ }

// app.ts: bring them in with import
import main, { name } from "./lib.js"; // default + named
import type { Shape } from "./lib.js"; // type-only, erased

// index.ts: re-export (a "barrel" file)
export { name } from "./lib.js";

Named export တွေကို brace နဲ့ import လုပ်ပြီး default export ကို brace မပါဘဲ import လုပ်ပါတယ်။ NodeNext project မှာ relative path တွေကို .js နဲ့ ရေးပါ၊ type-only import တွေကို import type နဲ့ မှတ်ပါ။

Import လည်း လုပ်၊ export လည်း လုပ်တဲ့ module တစ်ခု

math.ts နဲ့ app.ts လို file နှစ်ခုကို code block တစ်ခုထဲ ပေါင်းထည့်ရင် run လို့ မရပါဘူး၊ file တစ်ခုအဖြစ် run တဲ့အခါ ./math.js ကို ရှာမတွေ့လို့ပါ။ ဒါကြောင့် ဒီ example က file တစ်ခုတည်းနဲ့ run လို့ရတဲ့ module ပါ: Node ရဲ့ built-in node:path module ကနေ import လုပ်ပြီး ကိုယ်ပိုင် API ကို export လုပ်ပါတယ်။ တကယ့် project မှာ app.ts က import { labelFor } from "./file-label.js" လို့ ရေးပြီး ဒီ function ကို ယူသုံးမှာပါ။

file-label.ts
// file-label.ts: an ES module that imports from another module and exports its own API
import { basename, extname } from "node:path"; // named imports
import type { ParsedPath } from "node:path"; // type-only import: erased from the JS

export type FileLabel = { slug: string; ext: string }; // exported type

const MAX_SLUG = 20; // not exported: private to this module

export function labelFor(file: string): FileLabel {
  const ext = extname(file);
  const slug = basename(file, ext).toLowerCase().replace(/[^a-z0-9]+/g, "-");
  return { slug: slug.slice(0, MAX_SLUG), ext };
}

export function joinName(parts: Pick<ParsedPath, "name" | "ext">): string {
  return parts.name + parts.ext;
}

console.log(labelFor("src/My Report 2026.ts"));
console.log(joinName({ name: "math", ext: ".ts" }));

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 2import { basename, extname } from "node:path";

    Module တစ်ခုကနေ named import နှစ်ခု ယူပါတယ်။ နာမည်တွေက node:path က export လုပ်ထားတဲ့ နာမည်တွေနဲ့ အတိအကျ တူရပါမယ်။

  2. Line 3import type { ParsedPath } from "node:path";

    Type-only import ပါ။ ParsedPath ကို type annotation ထဲမှာပဲ သုံးတာကြောင့် ဒီ line တစ်ခုလုံး compile လုပ်ပြီးတဲ့ JavaScript ထဲက ပျောက်သွားပါတယ်။

  3. Line 5export type FileLabel = { slug: string; ext: string };

    Type တစ်ခုကို export လုပ်ပါတယ်၊ ဒါမှ တခြား file တွေက import type { FileLabel } နဲ့ ယူသုံးနိုင်မှာပါ။

  4. Line 7const MAX_SLUG = 20;

    export မပါတာကြောင့် ဒီ constant က ဒီ module ရဲ့ private အပိုင်းပါ။ တခြား file က import လုပ်ဖို့ ကြိုးစားရင် compile error တက်ပါမယ်။

  5. Lines 9-13export function labelFor(file: string): FileLabel {

    Module ရဲ့ public API ပါ။ Import လုပ်ထားတဲ့ helper တွေနဲ့ private constant ကို သုံးပြီး { slug, ext } ကို ပြန်ပေးပါတယ်။

You should see
{ slug: 'my-report-2026', ext: '.ts' }
math.ts

Package boundary: exports field

ကိုယ့် code က တခြား project တွေ install လုပ်မယ့် package တစ်ခု ဖြစ်လာတဲ့အခါ package.json ရဲ့ exports field က package ရဲ့ ရှေ့ကောင်တာ ဖြစ်လာပါတယ်။ အဲဒီမှာ စာရင်းသွင်းထားတဲ့ path တွေကိုပဲ import လုပ်လို့ရပြီး internal file တွေထဲ တိုက်ရိုက် ဝင်တဲ့ deep import တွေက fail ဖြစ်ပါတယ်။ moduleResolution က NodeNext ဒါမှမဟုတ် bundler ဖြစ်ရင် TypeScript က ဒီ field ကို လိုက်နာပြီး types condition က declaration file တွေကို ညွှန်ပေးပါတယ်။

package.json
{
  "name": "@acme/money",
  "type": "module",
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "default": "./dist/index.js"
    }
  }
}

Internal file တွေကို လွတ်လပ်စွာ ပြင်ခွင့်

Consumer တွေက "@acme/money" ကနေ import လုပ်လို့ ရပေမယ့် "@acme/money/dist/internal.js" ကို import လုပ်ရင် Node က ERR_PACKAGE_PATH_NOT_EXPORTED နဲ့ ငြင်းပါတယ်။ ဒါကြောင့် ဘယ်သူ့ကိုမှ မထိခိုက်ဘဲ internal file တွေကို ပြန်စီလို့ ရပါတယ်။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project မှာ module တွေကို ဘယ်လို စီကြလဲ

Node.js backend တစ်ခုမှာ feature တစ်ခုချင်းစီကို folder တစ်ခုစီ ခွဲထားလေ့ ရှိပါတယ်: users/ ထဲမှာ user.types.ts, user.service.ts, user.routes.ts စသဖြင့်ပါ။ Service file က type file ကို import type နဲ့ ယူပြီး routes file က service ရဲ့ named export တွေကိုပဲ သုံးပါတယ်။ Database client လို အရာတွေကိုတော့ module တစ်ခုထဲမှာ တစ်ခါပဲ ဆောက်ပြီး export လုပ်ပါတယ်။ Module code က ပထမဆုံး import လုပ်ချိန်မှာ တစ်ကြိမ်ပဲ run ပြီး cache ဖြစ်သွားတာကြောင့် import လုပ်သူတိုင်းက instance တစ်ခုတည်းကို မျှသုံးကြပါတယ်။

Shared code တွေကို monorepo ထဲမှာ package အဖြစ် ခွဲထားရင် package တစ်ခုစီရဲ့ exports field က တခြား team တွေ မှီခိုခွင့်ရှိတဲ့ အပိုင်းကို သတ်မှတ်ပေးပါတယ်။ index.ts barrel file တွေက public API ကို နေရာတစ်ခုတည်းကနေ re-export လုပ်ဖို့ အသုံးဝင်ပေမယ့် file ရာချီကို re-export လုပ်တဲ့ barrel ကြီးတွေက test နဲ့ bundler တွေကို နှေးစေနိုင်တာကြောင့် package boundary မှာပဲ သုံးတာ ကောင်းပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားများ

1. Extension မထည့်တာ ဒါမှမဟုတ် .ts လို့ ရေးတာ

NodeNext project မှာ "./math" ဆိုရင် error TS2835 တက်ပြီး ဟောင်းတဲ့ setting တွေမှာတော့ runtime ကျမှ ERR_MODULE_NOT_FOUND နဲ့ fail ဖြစ်ပါတယ်။ "./math.ts" ကို ရေးတာကလည်း tsc ကိုယ်တိုင် JavaScript မထုတ်တဲ့ setup တွေမှာ allowImportingTsExtensions လို option အထူးတွေနဲ့မှ ခွင့်ပြုပါတယ်။ Emit လုပ်မယ့် file ရဲ့ path ဖြစ်တဲ့ "./math.js" ကို ရေးပါ။

2. Default import နဲ့ named import ကို ရောတာ

math.ts က export function add လို့ named export လုပ်ထားရင် import add from "./math.js" က "has no default export" error ပေးပါတယ်၊ brace ပါတဲ့ import { add } လိုပါတယ်။ ပြောင်းပြန်အနေနဲ့ export default ကို brace နဲ့ import လုပ်ရင်လည်း error ဖြစ်ပါတယ်။ Export လုပ်တဲ့ ပုံစံနဲ့ import ပုံစံ ကိုက်ရပါမယ်။

3. File နှစ်ခု အပြန်အလှန် import လုပ်တာ

a.ts က b.ts ကို import လုပ်ပြီး b.ts ကလည်း a.ts ကို import လုပ်ရင် circular import ဖြစ်ပါတယ်။ တစ်ဖက်က module ရဲ့ top-level code run နေတုန်းမှာ နောက်တစ်ဖက်ရဲ့ const ဒါမှမဟုတ် class ကို သုံးမိရင် "Cannot access before initialization" ဆိုတဲ့ ReferenceError ရပါတယ်။ နှစ်ခုလုံး လိုတဲ့ code ကို တတိယ module တစ်ခုထဲ ရွှေ့ပါ။

Best Practices

အကောင်းဆုံး လုပ်ထုံးများ

  • Node project အသစ်တွေမှာ module နဲ့ moduleResolution ကို NodeNext ထားပြီး bundler project တွေမှာ "bundler" ကို သုံးပါ။
  • verbatimModuleSyntax ကို ဖွင့်ထားပါ၊ ဒါမှ type-only import တိုင်းကို import type နဲ့ ရှင်းရှင်းလင်းလင်း မှတ်ရမှာပါ။
  • Default export ထက် named export ကို ဦးစားပေးပါ၊ ရှာရလွယ်ပြီး rename လုပ်ရတာ ပိုလုံခြုံပါတယ်။
  • Package တစ်ခု publish လုပ်ရင် exports field နဲ့ public path တွေကို ကန့်သတ်ပါ။

Mini challenge

temperature.ts module တစ်ခု

Public API ကို export လုပ်ပြီး helper ကို private ထားတဲ့ module တစ်ခု ရေးပါ။ Built-in module တစ်ခုကနေ named import တစ်ခု သုံးပြီး ကိုယ့် function ကို self-check လုပ်ပါ။

  • type Reading = { city: string; celsius: number } ကို export လုပ်ပါ။
  • toFahrenheit(celsius) ကို named export အဖြစ် export လုပ်ပြီး ဒသမ တစ်နေရာထိ round လုပ်ပါ။
  • round1 helper ကို export မလုပ်ဘဲ private ထားပါ။
  • formatReading ကို default export အဖြစ် ထားပြီး "Yangon: 31.5 C / 88.7 F" ပုံစံ string ပြန်ပေးပါ။
  • node:assert/strict ကနေ strictEqual ကို import လုပ်ပြီး toFahrenheit(100) က 212 ဖြစ်ကြောင်း စစ်ပါ။
temperature.ts
// TODO: named import of strictEqual from "node:assert/strict"

// TODO: export type Reading with city (string) and celsius (number)

function round1(value: number): number {
  // TODO: round to one decimal place (keep this helper private)
  return value;
}

// TODO: export function toFahrenheit(celsius: number): number

// TODO: export default function formatReading(r: Reading): string

// TODO: self-check toFahrenheit(100) with strictEqual, then print two readings

ရလဒ်: Self-check အောင်မြင်ပြီး "Yangon: 31.5 C / 88.7 F" နဲ့ "Mandalay: 38 C / 100.4 F" ဆိုတဲ့ line နှစ်ကြောင်း print ထွက်ပါမယ်။

အရိပ်အမြွက် ကြည့်ရန်

F = C * 9 / 5 + 32 ပါ။ Math.round(value * 10) / 10 က ဒသမ တစ်နေရာထိ round လုပ်ပေးပါတယ်။ တကယ့် app.ts မှာဆိုရင် import formatReading, { toFahrenheit, type Reading } from "./temperature.js" လို့ ရေးမှာပါ။

အဖြေ ကြည့်ရန်
temperature.ts
import { strictEqual } from "node:assert/strict";

export type Reading = { city: string; celsius: number };

function round1(value: number): number {
  return Math.round(value * 10) / 10;
}

export function toFahrenheit(celsius: number): number {
  return round1((celsius * 9) / 5 + 32);
}

export default function formatReading(r: Reading): string {
  return `${r.city}: ${r.celsius} C / ${toFahrenheit(r.celsius)} F`;
}

strictEqual(toFahrenheit(100), 212); // throws if the formula is wrong
console.log(formatReading({ city: "Yangon", celsius: 31.5 }));
console.log(formatReading({ city: "Mandalay", celsius: 38 }));

Quick Quiz 1 / 3

math.ts မှာ export function add ရှိပါတယ်။ NodeNext project ရဲ့ app.ts မှာ ဘယ် import က မှန်လဲ?

Quick Quiz 2 / 3

import type { User } from "./user.js"; အတွက် emit လုပ်ထားတဲ့ JavaScript ထဲမှာ ဘာကျန်မလဲ?

Quick Quiz 3 / 3

package.json မှာ "exports": { ".": "./dist/index.js" } ရှိရင် import { x } from "pkg/dist/internal.js" က ဘာဖြစ်မလဲ?

မှတ်ထားရမယ့် အချက်များ

  • Module တစ်ခုထဲက နာမည်တွေက default အားဖြင့် private ဖြစ်ပြီး export လုပ်ထားတာတွေကိုပဲ import လုပ်လို့ ရပါတယ်။
  • NodeNext project မှာ relative import တွေကို emit လုပ်မယ့် .js path နဲ့ ရေးပါ၊ TypeScript က path ကို ပြန်မရေးပေးပါဘူး။
  • import type က type-only import တွေကို output ထဲက ဖယ်ပေးပါတယ်။
  • package.json ရဲ့ "type" က module format ကို၊ "exports" က package ရဲ့ public surface ကို သတ်မှတ်ပါတယ်။

Modules Reference — TypeScript

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။