A Basic Guide to Node Modules: CJS, ESM, AMD, and UMD
There are 4 types of node modules:
- CJS(CommonJS)
- ESM(es6)
- AMD(Asynchronous Module Definition)
- UMD(Universal Module Definition)
Here’s a comparison of the 4 types of modules.
| Feature | umd | cjs | esm | amd |
|---|---|---|---|---|
| Purpose | Universal module format | Server-side module format | JavaScript standard module format | Asynchronous module format |
| Supported environments | Browser and Node.js | Node.js | Modern browsers and Node.js (>= 13) | Browser |
| Syntax | Defines a factory function | Uses require and module.exports |
Uses import and export |
Uses define and require |
| Dynamic import | No | No | Yes | Yes |
| Asynchronous loading | No | No | Yes | Yes |
| Tree shaking | No | No | Yes | No |
| Dead code elimination | No | No | Yes | No |
| Native support | No | Yes (in Node.js) | Yes (in modern browsers and Node.js) | No |
| Interoperability | High (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) |
| Performance | Medium | Medium | High | Medium |
| Usage complexity | Medium | Low | Low | Medium |
| Popularity | Medium | High | Growing popularity | Declining popularity |
發佈時間
2023-5-13