Thuta Learning
Node.js
BasicWeb Developmentအခြေခံ

Modules (Require/Export)

Node.jsသင်ခန်းစာ 525 မိနစ်

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

  • module.exports နဲ့ function တွေကို export လုပ်ပြီး require() နဲ့ ပြန်ယူသုံးနိုင်ဖို့
  • Module တစ်ခုက ပထမ require မှာ တစ်ခါပဲ run ပြီး cache ထဲက object ကို ပြန်ပေးကြောင်း ရှင်းပြနိုင်ဖို့
  • exports နဲ့ module.exports ကွာခြားချက်ကို သိပြီး exports = {...} အမှားကို ရှောင်နိုင်ဖို့
  • "./file"၊ "node:fs"၊ "express" လို specifier တွေကို Node ဘယ်လို resolve လုပ်လဲ ခွဲခြားနိုင်ဖို့

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

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

Module ဆိုတာ ကြိုးခံပြီး ပိတ်ထားတဲ့ ကိရိယာသေတ္တာ တစ်လုံးလိုပါပဲ။ သေတ္တာထဲမှာ ကိရိယာ အများကြီး ရှိနိုင်ပေမယ့် အပြင်ကလူတွေ ယူသုံးလို့ရတာက သင် ပြတင်းပေါက်မှာ တင်ထားတဲ့ ကိရိယာတွေ (module.exports) ပဲ ဖြစ်ပါတယ်။

တခြား file တစ်ခုက require() နဲ့ "အဲဒီ သေတ္တာကို ပေးပါ" လို့ တောင်းတဲ့အခါ Node က သေတ္တာကို တစ်ခါပဲ ဖွင့်ပြီး ပြတင်းပေါက်မှာ ရှိတာတွေကို ပေးပါတယ်။ နောက်တစ်ခါ ထပ်တောင်းရင် အရင်ပေးခဲ့တဲ့ ကိရိယာတွေ အတိုင်းပဲ ပြန်ပေးပါတယ်၊ copy အသစ် မဟုတ်ပါဘူး။ သေတ္တာထဲမှာ ကျန်ခဲ့တာတွေကို ဘယ်သူမှ မမြင်ရလို့ ဘယ်သူ့ကိုမှ အနှောင့်အယှက် မပေးဘဲ ပြန်စီလို့ရပါတယ်။

Problem → Why → Solution

ပြဿနာ: file တစ်ခုတည်းမှာ code ထောင်ချီ

Project စစချင်းမှာ code အားလုံးကို app.js တစ်ခုတည်းထဲ ရေးတာ လွယ်ပါတယ်။ ခြောက်လလောက်ကြာတော့ line ထောင်ချီဖြစ်လာပြီး function တစ်ခုကို ရှာရ ခက်လာ၊ variable နာမည်တွေ တိုက်လာ၊ တစ်နေရာ ပြင်လိုက်ရင် မဆိုင်တဲ့ နေရာ ပျက်သွားပါတယ်။ ဒါကို ခွဲဖို့ ကြိုးစားတဲ့ ပထမအကြိမ်မှာလည်း အောက်ကလို အမှားနှစ်ခု ဖြစ်လေ့ရှိပါတယ်။

app.js
// mathTools.js ended with:  exports = { add, multiply };
const mathTools = require("mathTools"); // no ./ in front

console.log(mathTools.add(5, 3));
// Error: Cannot find module 'mathTools'
// ...and after adding ./ : TypeError: mathTools.add is not a function

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

./ မပါတဲ့ specifier ကို Node က package နာမည်လို့ ယူပြီး node_modules ထဲ ရှာပါတယ်။ exports = {...} ကလည်း local variable ကိုပဲ ပြောင်းလိုက်တာမို့ require() ပြန်ပေးတဲ့ module.exports က ဗလာ object အတိုင်း ကျန်နေပါတယ်။ CommonJS ရဲ့ စည်းမျဉ်း အနည်းငယ်ကို သိရင် ဒီ အမှားနှစ်ခုလုံး ပျောက်ပါတယ်။

Core concept: file တစ်ခု = scope တစ်ခု၊ module.exports = public API

CommonJS က Node ရဲ့ မူလ module system ပါ။ Node က .js file တစ်ခုကို run မလုပ်ခင် function တစ်ခုထဲ ထုပ်လိုက်ပါတယ် — (function (exports, require, module, __filename, __dirname) { ...သင့် code... })။ ဒါကြောင့် file ထဲမှာ ကြေညာတဲ့ variable တိုင်းက အဲဒီ file ရဲ့ private ဖြစ်ပြီး require, module, __dirname တို့ကလည်း global မဟုတ်ဘဲ function parameter တွေ ဖြစ်ပါတယ်။

require("./mathTools") ကို ခေါ်ရင် Node က path ကို ရှာ၊ file ကို တစ်ခါ run ပြီး module.exports ထဲမှာ ရှိတာကို ပြန်ပေးပါတယ်။ Module ကို ပြည့်ပြည့်စုံစုံ resolve လုပ်ထားတဲ့ filename နဲ့ cache လုပ်ထားလို့ ဒုတိယ require က file ကို ထပ်မ run တော့ဘဲ object တစ်ခုတည်းကို ပြန်ပေးပါတယ်။ ဒါကြောင့် database connection pool လို တစ်ခုတည်းသာ ရှိသင့်တဲ့ အရာကို module ထဲမှာ ဖန်တီးထားရင် app တစ်ခုလုံးက share သုံးကြပါတယ်။ require() က synchronous ဖြစ်လို့ file ကို load ပြီးမှ နောက် line ကို ဆက်သွားပါတယ်။

exports က module.exports ကို ညွှန်ထားတဲ့ shortcut variable ပဲ ဖြစ်ပါတယ်။ exports.add = add က object ထဲကို property ထည့်လို့ အလုပ်ဖြစ်ပေမယ့် exports = { add } ကတော့ shortcut ကို object အသစ်ဆီ ရွှေ့လိုက်ရုံပါ — Node ကတော့ module.exports ကိုပဲ ပြန်ပေးပါတယ်။ Object တစ်ခုလုံး export လုပ်ချင်ရင် module.exports = { add } လို့ ရေးပါ။

Node က file တစ်ခုကို CommonJS အဖြစ် ဘယ်အချိန် ဆက်ဆံလဲ — .cjs file တွေက အမြဲ CommonJS ဖြစ်ပြီး .js file တွေကတော့ အနီးဆုံး package.json မှာ "type": "module" မရှိရင် CommonJS ပါ။ module.exports ထဲကို object သာမက function တစ်ခု၊ class တစ်ခု ဒါမှမဟုတ် string တစ်ခုကိုပါ ထည့်လို့ရပါတယ် — module.exports = class Cart {...} ဆိုရင် require() က class ကို တိုက်ရိုက် ပြန်ပေးပါတယ်။ JSON file တွေကိုလည်း require("./data.json") နဲ့ parse လုပ်ပြီးသား object အဖြစ် load လုပ်လို့ရပါတယ်။

SpecifierNode ဘယ်မှာ ရှာလဲ
"node:fs"Node ထဲ ပါပြီးသား built-in module (ပိုရှင်းလို့ ဒီပုံစံကို သုံးပါ)
"./mathTools"require ခေါ်တဲ့ file ရှိတဲ့ folder ကနေ စ — mathTools, .js, .json, ပြီးရင် folder/index.js
"express"node_modules/express ကို လက်ရှိ folder ကနေ အပေါ်ဘက် folder တွေဆီ တက်ရှာ
မှတ်ထားရမယ့် ပုံစံ: require() ရဲ့ အဆင့်တွေ
  1. Specifier ကို file path အဖြစ် resolve
  2. Cache ထဲမှာ ရှိပြီးသားလား စစ်
  3. မရှိရင် file ကို wrapper ထဲမှာ run
  4. module.exports ကို cache ပြီး ပြန်ပေး

File ရဲ့ code က process တစ်ခုမှာ တစ်ခါပဲ run ပါတယ်။

အထွေထွေ ပုံစံ: CommonJS export နဲ့ import

javascript
// some-module.js
const privateValue = 1;               // stays inside this file
function publicName(param) { /* ... */ }
module.exports = { publicName };

// consumer.js
const { publicName } = require("./some-module");
const builtIn = require("node:module-name");

Module ဘက်မှာ module.exports ကို တစ်ခါ assign လုပ်ပြီး ပြင်ပကို ပေးချင်တာတွေပဲ ထည့်ပါ။ အသုံးပြုသူ ဘက်မှာ require() ရဲ့ ရလဒ်ကို destructuring နဲ့ ခွဲယူလို့ရပါတယ်။ ကိုယ့် file တွေအတွက် ./ ကို အမြဲ ထည့်ပြီး built-in တွေအတွက် node: ကို ထည့်ပါ။

Example: module တစ်ခုကို အမှန်တကယ် load ပြီး cache ကို ကြည့်မယ်

File နှစ်ခုကို command တစ်ခုတည်းနဲ့ စမ်းနိုင်အောင် ဒီ script က mathTools.js ကို temporary folder ထဲမှာ ရေးပြီး require() နဲ့ နှစ်ခါ load လုပ်ပါတယ်။ Module ရဲ့ log က တစ်ခါပဲ ပေါ်တာ၊ require နှစ်ခုက object တစ်ခုတည်းကို ပြန်ပေးတာနဲ့ private variable ကို အပြင်ကနေ မမြင်ရတာကို ကြည့်ပါ။ ပြီးရင် temporary folder ကို ဖျက်ပါတယ်။

modules-demo.js
// modules-demo.js - CommonJS: module.exports and require()
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");

// Write a real module file into a fresh temporary folder.
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cjs-demo-"));
const mathFile = path.join(dir, "mathTools.js");
fs.writeFileSync(mathFile, `
console.log("mathTools.js runs (only once)");
let calls = 0; // private to this file
function add(a, b) { calls++; return a + b; }
function multiply(a, b) { calls++; return a * b; }
module.exports = { add, multiply, callCount: () => calls };
`);

const mathTools = require(mathFile);
const sameModule = require(mathFile);

console.log(mathTools.add(5, 3));
console.log(mathTools.multiply(5, 3));
console.log("Second require returns the same object:", mathTools === sameModule);
console.log("Calls so far:", mathTools.callCount());
console.log("Can this file see calls?", typeof calls);

fs.rmSync(dir, { recursive: true, force: true });

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

  1. Lines 2-4const fs = require("node:fs");

    Built-in module တွေကို node: prefix နဲ့ require လုပ်ပါတယ်။ fs, os, path တို့ကို temporary file တစ်ခု ဖန်တီးဖို့ပဲ သုံးတာပါ။

  2. Line 14module.exports = { add, multiply, callCount: () => calls };

    ဒါက mathTools.js ရဲ့ public API ပါ။ calls variable ကိုယ်တိုင်ကို export မလုပ်ဘဲ ဖတ်လို့ပဲရတဲ့ callCount() function ကို ပေးထားလို့ အပြင်ကနေ ပြင်လို့မရပါဘူး။

  3. Lines 17-18const sameModule = require(mathFile);

    ပထမ require က file ကို run လို့ "runs (only once)" log ပေါ်ပါတယ်။ ဒုတိယ require ကတော့ cache ထဲက object ကို ပြန်ပေးလို့ log ထပ်မပေါ်ပါဘူး။ Project ထဲမှာဆို require("./mathTools") လို့ relative path နဲ့ ရေးပါတယ်။

  4. Line 22mathTools === sameModule

    true ဖြစ်တာက variable နှစ်ခုလုံး object တစ်ခုတည်းကို ညွှန်နေလို့ပါ။ ဒါကြောင့် calls count က require နှစ်ခုကြားမှာ share ဖြစ်နေပါတယ်။

  5. Line 24typeof calls

    calls က mathTools.js ရဲ့ wrapper function ထဲမှာ ကြေညာထားလို့ ဒီ file ကနေ မမြင်ရဘဲ "undefined" ရပါတယ်။

You should see
mathTools.js runs (only once)
8
15
Second require returns the same object: true
Calls so far: 2
Can this file see calls? undefined

Project ထဲမှာ ရေးမယ့် file နှစ်ခု ပုံစံ

Project အစစ်မှာ temporary file မလိုပါဘူး — mathTools.js နဲ့ app.js ကို folder တစ်ခုတည်းမှာ ထားပြီး node app.js နဲ့ run ပါတယ်။ "./mathTools" ရဲ့ ./ က app.js ရှိတဲ့ folder ကို ဆိုလိုပြီး terminal ရဲ့ လက်ရှိ folder ကို မဆိုလိုပါဘူး။ .js extension ကို ချန်ခဲ့လည်း require() က ဖြည့်ရှာပေးပါတယ်။

mathTools.js
function add(a, b) {
  return a + b;
}

function multiply(a, b) {
  return a * b;
}

module.exports = { add, multiply };
app.js
const { add, multiply } = require("./mathTools");

console.log(add(5, 3));      // 8
console.log(multiply(5, 3)); // 15

exports နဲ့ module.exports ကို တိုက်ရိုက် စမ်းကြည့်ရအောင်

ဒီ snippet က shortcut variable ကို object အသစ်နဲ့ အစားထိုးလိုက်ရင် ဘာဖြစ်လဲ ပြပါတယ်။ module.exports မှာ add တစ်ခုပဲ ကျန်ပြီး multiply ကတော့ ဘယ်သူမှ မမြင်ရတဲ့ local object ထဲမှာ ပျောက်သွားပါတယ်။

exports-alias.js
// exports-alias.js - why "exports = ..." silently does nothing
const { strict: assert } = require("node:assert");

assert.ok(exports === module.exports); // two names, one object
exports.add = (a, b) => a + b; // adds a property: works
exports = { multiply: (a, b) => a * b }; // rebinds the local name only

console.log("module.exports keys:", Object.keys(module.exports));
console.log("local exports keys:", Object.keys(exports));

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

Real project တွေမှာ CommonJS ဘယ်မှာ တွေ့ရလဲ

Node ecosystem က CommonJS နဲ့ ကြီးထွားလာခဲ့လို့ npm package အများအပြားနဲ့ ရှိပြီးသား Express app တွေ အများစုက require() ကို သုံးနေဆဲပါ။ .eslintrc.cjs, jest.config.js လို tool configuration file တွေမှာလည်း module.exports = {...} ကို မကြာခဏ တွေ့ရပါတယ်။ ES module ကို ပိုသုံးလာကြပေမယ့် legacy code ဖတ်ဖို့နဲ့ debug လုပ်ဖို့ CommonJS ကို နားလည်ထားဖို့ လိုပါတယ်၊ job interview တွေမှာလည်း မေးနေဆဲပါ။

Project တွေကို ပုံမှန်အားဖြင့် တာဝန်အလိုက် module ခွဲပါတယ် — routes/users.js က HTTP route တွေ၊ services/userService.js က business logic၊ db.js က database connection တစ်ခုကို export လုပ်ပြီး module cache ကြောင့် app တစ်ခုလုံးက connection တစ်ခုတည်းကို share သုံးပါတယ်။ Public API ကို သေးသေးလေး ထားလေ test ရေးရ လွယ်လေ၊ internal code ကို ပြင်တဲ့အခါ အသုံးပြုသူ file တွေ ပျက်မှာ မစိုးရိမ်ရလေ ဖြစ်ပါတယ်။

အမှားများ

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

1. ကိုယ့် file အတွက် ./ မေ့တာ

require("mathTools") ကို Node က package နာမည်အဖြစ် ယူပြီး node_modules ထဲ ရှာလို့ "Cannot find module 'mathTools'" ရပါတယ်။ Project file တွေကို "./mathTools" ဒါမှမဟုတ် "../lib/mathTools" လို relative path နဲ့ အမြဲ require လုပ်ပါ။

2. exports ကို object အသစ်နဲ့ အစားထိုးတာ

exports = { add } က error မပြဘဲ ဘာမှ export မဖြစ်ပါဘူး၊ အသုံးပြုသူ file မှာ "add is not a function" ကျမှ ပေါ်လာပါတယ်။ Property တစ်ခုချင်း ထည့်ရင် exports.add = add၊ object တစ်ခုလုံးဆိုရင် module.exports = { add } လို့ ရေးပါ။

3. Module နှစ်ခု အပြန်အလှန် require လုပ်တာ

a.js က b.js ကို require လုပ်ပြီး b.js ကလည်း a.js ကို require လုပ်ရင် (circular dependency) b.js က a.js ရဲ့ မပြီးသေးတဲ့ exports ကို ရပြီး function တွေ undefined ဖြစ်နေတတ်ပါတယ်။ နှစ်ခုလုံး လိုတဲ့ code ကို တတိယ module တစ်ခုထဲ ရွှေ့ပါ။

Best Practices

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

  • require() ခေါ်တာတွေကို file ရဲ့ ထိပ်ဆုံးမှာ စုထားပါ၊ ဒါမှ file တစ်ခုက ဘာတွေကို မှီခိုလဲ တစ်ချက်ကြည့်ရုံနဲ့ သိနိုင်ပါတယ်။
  • module.exports ကို file အဆုံးမှာ တစ်ခါတည်း assign လုပ်ပါ၊ public API ကို နေရာတစ်ခုတည်းမှာ ဖတ်လို့ရပါတယ်။
  • Module တစ်ခုကို တာဝန်တစ်ခုတည်း ပေးပါ — နာမည်ကို "and" နဲ့ ပေးချင်လာရင် ခွဲသင့်ပြီ ဆိုတဲ့ လက္ခဏာပါ။
  • Module ထဲမှာ require လုပ်တဲ့အချိန် side effect (server စတာ၊ file ရေးတာ) မလုပ်ပါနဲ့၊ function တစ်ခုထဲ ထည့်ပြီး ခေါ်တဲ့သူကို ဆုံးဖြတ်ခွင့်ပေးပါ။

Mini challenge

Mini challenge: ကိုယ့်ကိုယ်ကို စမ်းနိုင်တဲ့ calculator module

add နဲ့ divide နှစ်ခုကိုပဲ export လုပ်တဲ့ calculator.js ကို ရေးပါ။ Input စစ်တဲ့ helper က private ဖြစ်ရပါမယ်။ File ကို node calculator.js နဲ့ တိုက်ရိုက် run ရင် self-test လုပ်ပြီး တခြား file က require လုပ်ရင်တော့ ဘာမှ print မထုတ်ရပါဘူး။

  • assertNumber() helper ကို export မလုပ်ရ
  • number မဟုတ်တာ ပေးရင် TypeError၊ သုညနဲ့ စားရင် RangeError throw ရမယ်
  • module.exports = { add, divide } ပဲ ရှိရမယ်
  • require.main === module ဖြစ်မှသာ self-test ကို run ရမယ်
calculator.js
// calculator.js - a CommonJS module that can also test itself
const assert = require("node:assert/strict");

function assertNumber(value, name) {
  // TODO: throw a TypeError unless value is a real number (not NaN)
}

function add(a, b) {
  // TODO: validate both arguments, then return the sum
}

function divide(a, b) {
  // TODO: validate, throw a RangeError when b is 0, return a / b
}

// TODO: export only add and divide

// TODO: if this file is the entry point (require.main === module):
//   assert add(2, 3) is 5, print a few results, catch and print two errors,
//   and print the exported names

ရလဒ်: node calculator.js နဲ့ run ရင် ဒီလို ထွက်ရပါမယ်: add(2, 3) = 5 divide(10, 4) = 2.5 RangeError: Cannot divide by zero TypeError: a must be a number, got string Public API: add, divide

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

require.main က node နဲ့ တိုက်ရိုက် run ခဲ့တဲ့ module ကို ညွှန်ပါတယ်။ ဒီ file ကို တခြား file က require လုပ်ရင် require.main က အဲဒီ file ဖြစ်နေလို့ condition false ဖြစ်ပါတယ်။ Error တွေကို err.name နဲ့ err.message နဲ့ print ထုတ်ပါ။

အဖြေ ကြည့်ရန်
calculator.js
// calculator.js - a CommonJS module that can also test itself
const assert = require("node:assert/strict");

function assertNumber(value, name) {
  if (typeof value !== "number" || Number.isNaN(value)) {
    throw new TypeError(`${name} must be a number, got ${typeof value}`);
  }
}

function add(a, b) {
  assertNumber(a, "a");
  assertNumber(b, "b");
  return a + b;
}

function divide(a, b) {
  assertNumber(a, "a");
  assertNumber(b, "b");
  if (b === 0) throw new RangeError("Cannot divide by zero");
  return a / b;
}

module.exports = { add, divide };

// True only when run as `node calculator.js`, not when another file requires it.
if (require.main === module) {
  assert.equal(add(2, 3), 5);
  console.log("add(2, 3) =", add(2, 3));
  console.log("divide(10, 4) =", divide(10, 4));
  for (const bad of [() => divide(1, 0), () => add("2", 3)]) {
    try {
      bad();
    } catch (err) {
      console.log(`${err.name}: ${err.message}`);
    }
  }
  console.log("Public API:", Object.keys(module.exports).join(", "));
}

Quick Quiz 1 / 3

counter.js မှာ let count = 0; module.exports = { next: () => ++count }; ရှိတယ်။ a.js နဲ့ b.js နှစ်ခုလုံးက require("./counter") လုပ်ပြီး next() ကို တစ်ခါစီ ခေါ်တယ် (a အရင်)။ b ရဲ့ next() က ဘာပြန်မလဲ။

Quick Quiz 2 / 3

helpers.js က exports = { slugify }; နဲ့ ဆုံးတယ်။ app.js မှာ require("./helpers").slugify က ဘာလဲ။

Quick Quiz 3 / 3

src/app.js ထဲမှာ require("./config.json") ရေးထားတယ်။ Terminal က project root မှာ ရှိနေပြီး node src/app.js နဲ့ run တယ်။ Node က config.json ကို ဘယ်မှာ ရှာမလဲ။

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

  • CommonJS file တစ်ခုစီက wrapper function ထဲမှာ run လို့ variable တွေက private ဖြစ်ပါတယ်။
  • require() က module.exports ကို ပြန်ပေးပြီး module ကို ပထမအကြိမ်မှာ တစ်ခါပဲ run ကာ cache လုပ်ပါတယ်။
  • exports.x = ... က အလုပ်ဖြစ်ပေမယ့် exports = {...} က ဘာမှ export မဖြစ်လို့ module.exports = {...} ကို သုံးပါ။
  • ကိုယ့် file တွေကို ./ နဲ့၊ built-in တွေကို node: နဲ့ require လုပ်ပြီး package တွေကို နာမည်ချည်းနဲ့ require လုပ်ပါ။

CommonJS modules — Node.js

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

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

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

Modules (Require/Export) | Thuta Learning