Universal Module Definition

UMD, or Universal Module Definition, is a module definition format that aims to be compatible with both CommonJS and AMD. It also allows you to export the module as a global variable that you can include in your application through a simple <script> tag.

It does this by wrapping the modules in a boilerplate that checks the environment to detect how the module is used, and produces the correct exported object.

For example, the preceding greeter example would look like this with UMD:

// greeter.js(function (root, factory) {  // Requirements are defined here}(this, function () {  function helloWorld(name) {    process.stdout.write(`hello ${name}!\n`  };  // Whatever you return is exposed  return {    helloWorld: helloWorld

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.