A Basic Guide to Node Modules: CJS, ESM, AMD, and UMD

There are 4 types of node modules:

  1. CJS(CommonJS)
  2. ESM(es6)
  3. AMD(Asynchronous Module Definition)
  4. UMD(Universal Module Definition)

Here’s a comparison of the 4 types of modules.

Featureumdcjsesmamd
PurposeUniversal module formatServer-side module formatJavaScript standard module formatAsynchronous module format
Supported environmentsBrowser and Node.jsNode.jsModern browsers and Node.js (>= 13)Browser
SyntaxDefines a factory functionUses require and module.exportsUses import and exportUses define and require
Dynamic importNoNoYesYes
Asynchronous loadingNoNoYesYes
Tree shakingNoNoYesNo
Dead code eliminationNoNoYesNo
Native supportNoYes (in Node.js)Yes (in modern browsers and Node.js)No
InteroperabilityHigh (works with AMD, CJS, and globals)Medium (works with ESM using import())Medium (works with CJS using import())Low (mainly works with other AMD modules)
PerformanceMediumMediumHighMedium
Usage complexityMediumLowLowMedium
PopularityMediumHighGrowing popularityDeclining popularity
cmd + /