CommonJS Vs ESM Vs AMD Vs UMD
Both CommonJS (CJS) and ECMAScript Module (ESM) are used to modulise our code:
- Prevent variable to expose globally
- By default variables in JS will be exported globally unless it's wrapped in a function. However with module, the variables will be private to the file.
- Import code from different files
AMD (Asynchronous Module Definition) and UMD provide some other standard.
- AMD (Asynchronous Module Definition) is an API set to define how would a browser load a package asynchronously.
- Implement of AMD (Asynchronous Module Definition) is RequireJS
- UMD define a pattern of how an NPM package should be compile so that it works both with CommonJS (CJS) and AMD (Asynchronous Module Definition)
- However UMD doesn't work with ECMAScript Module (ESM)