CommonJS vs ES Modules
CommonJS uses require() and module.exports, while ES modules use import and export. Node.js supports both systems, but they have different loading and interoperability semantics.
ES modules are standardized by ECMAScript and support static structure that tooling can analyze. CommonJS is historically important in the Node.js ecosystem and uses a runtime-oriented loading model.
CommonJS and ESM
// CommonJS
const path = require('node:path');
module.exports = { path };
// ES Modules
import path from 'node:path';
export { path };💡 The examples demonstrate the two module systems. Exact interoperability behavior depends on the runtime and package configuration.
Key Takeaways
- CommonJS and ES modules are distinct module systems.
- ES modules use import/export syntax.
- Node.js supports both with configuration-dependent interoperability.
Knowledge Check
Which syntax belongs to CommonJS?