| History | CommonJS modules “are the original way to package JavaScript code for Node.js” and have been supported since Node.js’s original release in 2009. | ESM (ECMAScript Modules) “are the official standard format to package JavaScript code for reuse.” and were first introduced with ES2015 (ES6) in 2015. |
| Loading/Execution | Synchronous | Asynchronous |
| Unique characteristics | Uses __filename, __dirname, and NODE_PATH variables | Uses top-level await |
| Imports | const module = require('file-path'); | import defaultThing, { namedThing } from 'file-path'; |
| Exports | module.exports=defaultThing, exports=readOnlyThing, module.exports.name=namedThing, exports.name=readOnlyNamedThing | export default defaultThing, export namedThing |
package.json - “type” | "type": "commonjs" | "type": "module" |
package.json - entry point | "entry": "index.js" | "exports": "./index.js" |
| File Extensions | .js & .ts by default or if "type": "commonjs" or commonjs syntax is detected. Also .cjs, .cts | .js & .ts if "type": "module" or ESM syntax detected. Also .mjs, .mts |
| Runtime Support | All Node.js versions | Node.js V12 or higher |
| Strict Mode | Requires use strict at top of file | Uses strict mode by default |
| This | this = exports | this = undefined |