JavaScript
CommonJS vs. ES Modules (ESM)
The main differences between CommonJS and ES Modules are:
- Import
- CommonJS:
const fs = require("fs");
- ES Modules:
import fs from "fs";
- CommonJS:
- Export
- CommonJS:
module.exports = { fs };
- ES Modules:
export { fs };
- CommonJS:
All modern browsers, and Node.js 12+ now support ES Modules. CommonJS and extension .cjs
should be avoided in favor of ES Modules and extension .mjs
whenever possible.