Junior #module
CommonJS và ES Modules khác nhau thế nào?
⚡ Trả lời nhanh
CommonJS (require/module.exports) load đồng bộ, động; ESM (import/export) là chuẩn của ngôn ngữ, phân tích tĩnh nên hỗ trợ tree-shaking và top-level await.
// CommonJS
const fs = require('fs');
module.exports = { run };
// ESM (package.json: "type": "module" hoặc đuôi .mjs)
import fs from 'node:fs';
export const run = () => {};
const m = await import('./lazy.js'); // import động