A brief look into CJS vs ESM.
Common JS(CJS) and EcmaScript Modules (ESM) are not the only module systems in the JS ecosystem. But we can consider them the two most important ones.
CJS is the most known system. But with the arrival of ESM we need to start checking its differences.
- uses
require
andmodule.exports
as API - works synchronously
- can use ESM files if these have
.mjs
extension - you can use
import
only asdynamic import
- looks like ESM
- uses
export
andimport
as API - translates/compiles to CJS in Node.js
- transpiled code with Babel
- Typescript sometimes 😅
- uses
export
andimport
as API - works asynchronously
- can use CJS files if these have
.cjs
extension
What Are CJS, AMD, UMD, and ESM in Javascript?