0Pricing
Node.js Backend Development Bootcamp · บทเรียน

อธิบายระบบโมดูลของ Node.js

สำรวจโมดูล CommonJS โมดูล ES และวิธีนำเข้าและส่งออกความสามารถข้ามไฟล์ใน Node.js

อธิบายระบบโมดูลของ Node.js เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Node.js Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Organize Your Code with Modules

As apps grow, one giant file gets messy. Modules split code into small, reusable, focused pieces. Node supports two systems: CommonJS and ES Modules.

CommonJS: `require()` & `module.exports`

CommonJS is Node's original module system: require() pulls code in, module.exports pushes code out. Import what you need, export what you offer.

CommonJS: Exporting a Function

Here's mathUtils.js. Whatever you assign to module.exports is what other files can import from it — here, just the add function.

const add = (a, b) => a + b;
const subtract = (a, b) => a - b;

module.exports = add; // Only 'add' is exported

CommonJS: Importing and Using

In another file, require('./mathUtils.js') pulls in that exported function. Keep both files in the same folder and run node app.js.

const addFunction = require('./mathUtils.js');

console.log("Using the imported function:");
console.log(`5 + 3 = ${addFunction(5, 3)}`);
console.log(`10 + 2 = ${addFunction(10, 2)}`);

CommonJS: Exporting Multiple Items

Need to export several things? Assign an object to module.exports holding all of them — a clean way to group related utilities.

const greet = (name) => `Hello, ${name}!`;
const farewell = (name) => `Goodbye, ${name}!`;

module.exports = {
  greetUser: greet,
  sayGoodbye: farewell
};

CommonJS: Importing Multiple Items

When a module exports an object, grab pieces with destructuring: const { greetUser, sayGoodbye } = require(...). Cleaner and clearer than dot access.

const { greetUser, sayGoodbye } = require('./utils.js');

console.log(greetUser('Alice'));
console.log(sayGoodbye('Bob'));

ES Modules: The Modern Standard

ES Modules (ESM) are JavaScript's official standard, using import/export. They enable tree-shaking — set "type": "module" or use .mjs to opt in.

ESM: Named Exports & Imports

With named exports, you export values by name and import them in matching { } braces — making it obvious exactly which parts you're using.

export const add = (a, b) => a + b;
export const multiply = (a, b) => a * b;

// app.mjs (main entry point)
// To run this: ensure package.json has "type": "module"
// and run with `node app.mjs`
import { add, multiply } from './calculator.mjs';

console.log(`2 + 4 = ${add(2, 4)}`);
console.log(`5 * 6 = ${multiply(5, 6)}`);

ESM: Default Exports

A module can have one default export, usually its main feature. Import it under any name you like, no braces needed.

const logMessage = (msg) => console.log(`[LOG] ${msg}`);
export default logMessage;

// main.mjs (main entry point)
// To run this: ensure package.json has "type": "module"
// and run with `node main.mjs`
import myLogger from './logger.mjs';

myLogger('This is a default message.');
myLogger('Another important event.');

Quick Check: Module Systems

You've learned about CommonJS and ES Modules. Let's test your understanding!

Recap: Node.js Modules

You've explored Node modules: CommonJS (require/module.exports, the classic) and ESM (import/export, the modern standard). Both keep apps organized.

คำถามที่พบบ่อย

บทเรียน “อธิบายระบบโมดูลของ Node.js” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “อธิบายระบบโมดูลของ Node.js” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “อธิบายระบบโมดูลของ Node.js”

สำรวจโมดูล CommonJS โมดูล ES และวิธีนำเข้าและส่งออกความสามารถข้ามไฟล์ใน Node.js คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Node.js Backend Development Bootcamp หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Node.js Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “อธิบายระบบโมดูลของ Node.js” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Node.js Backend Development Bootcamp นี้ได้ไหม

ได้ บทเรียน Node.js Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รู้จัก Node.js และ NPM
  2. อธิบายระบบโมดูลของ Node.js
  3. JavaScript แบบอะซิงโครนัสและลูปเหตุการณ์
  4. การทำงานกับระบบไฟล์และสตรีม
← กลับไปที่ Node.js Backend Development Bootcamp