Skip to content

Instantly share code, notes, and snippets.

@h0nza
Forked from ostinlviv/module-loading.md
Created January 8, 2022 20:48
Show Gist options
  • Save h0nza/ea0f5f7ed91ceae6d3eb48c7f2312cc0 to your computer and use it in GitHub Desktop.
Save h0nza/ea0f5f7ed91ceae6d3eb48c7f2312cc0 to your computer and use it in GitHub Desktop.
Module loading

js-modules

CommonJS

The CommonJS format is used in Node.js and uses require and module.exports to define dependencies and modules. CommonJS modules were designed with server development in mind. Naturally, the API is synchronous. In other words, modules are loaded at the moment and in the order they are required inside a source file.

AMD

The AMD format is used in browsers and uses a define function to define modules. The main difference between AMD and CommonJS lies in its support for asynchronous module loading. Asynchronous loading is made possible by using JavaScript's traditional closure idiom: a function is called when the requested modules are finished loading. Module definitions and importing a module is carried by the same function: when a module is defined its dependencies are made explicit.

UMD

The UMD format can be used both in the browser and in Node.js.

ES6

ES6 modules are syntactically pleasing and compatible with both synchronous and asynchronous modes of operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment