- Used by: Node.js
- Syntax:
require()
/module.exports
- Example:
const fs = require('fs'); module.exports = someFunction;
- Used by: Older browsers (via RequireJS)
- Syntax:
define()
- Example:
define(['dep'], function(dep) { return function() {}; });
- Used by: Libraries that work everywhere (browser, Node)
- Syntax: A wrapper that supports CJS, AMD, or globals
- Goal: Compatibility
- Used by: Modern browsers & Node.js
- Syntax:
import
/export
- Example:
import fs from 'fs'; export default someFunction;
- Used by: Older scripts, no module system
- Syntax: Self-running function
- Example:
(function() { // code here })();