Skip to content

Instantly share code, notes, and snippets.

@dotspencer
Last active April 5, 2025 14:17
Show Gist options
  • Save dotspencer/3838472ccf71296e651dae3d3d9bb780 to your computer and use it in GitHub Desktop.
Save dotspencer/3838472ccf71296e651dae3d3d9bb780 to your computer and use it in GitHub Desktop.
JavaScript module formats -- CJS, AMD, UMD, ESM, and IIFE

🟀 CJS (CommonJS)

  • Used by: Node.js
  • Syntax: require() / module.exports
  • Example:
    const fs = require('fs');
    module.exports = someFunction;

🟠 AMD (Asynchronous Module Definition)

  • Used by: Older browsers (via RequireJS)
  • Syntax: define()
  • Example:
    define(['dep'], function(dep) {
      return function() {};
    });

βšͺ UMD (Universal Module Definition)

  • Used by: Libraries that work everywhere (browser, Node)
  • Syntax: A wrapper that supports CJS, AMD, or globals
  • Goal: Compatibility

🟒 ESM (ECMAScript Modules)

  • Used by: Modern browsers & Node.js
  • Syntax: import / export
  • Example:
    import fs from 'fs';
    export default someFunction;

🟣 IIFE (Immediately Invoked Function Expression)

  • Used by: Older scripts, no module system
  • Syntax: Self-running function
  • Example:
    (function() {
      // code here
    })();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment