← back

JavaScript Module Definitions: CJS, AMD, UMD, and ESM

CJS

//importing 
const doSomething = require('./doSomething.js'); 

//exporting
module.exports = function doSomething(n) {
  // do something
}

AMD

define(['dep1', 'dep2'], function (dep1, dep2) {
    //Define the module value by returning a value.
    return function () {};
});

UMD

(function (root, factory) {
    if (typeof define === "function" && define.amd) {
        define(["jquery", "underscore"], factory);
    } else if (typeof exports === "object") {
        module.exports = factory(require("jquery"), require("underscore"));
    } else {
        root.Requester = factory(root.$, root._);
    }
}(this, function ($, _) {
    // this is where I defined my module implementation

    var Requester = { // ... };

    return Requester;
}));

ESM

<script type="module">
  import {func1} from 'my-lib';

  func1();
</script>

Summary

Material copied from: https://dev.to/iggredible/what-the-heck-are-cjs-amd-umd-and-esm-ikm