### Module Formats The following module formats are supported: * `esm`: [ECMAScript Module](#es-modules) * `cjs`: [CommonJS](#commonjs) * `amd`: [Asynchronous Module Definition](#amd) * `global`: [Global shim module format](#globals) * `system`: [System.register](system-api.md#systemregister-name-deps-declare) or [System.registerDynamic](system-api.md#systemregisterdynamic-name-deps-executingrequire-declare) compatibility module format * (Need more? Check the [plugins](plugins.md).) The module format can be set via meta configuration: ```javascript SystemJS.config({ meta: { './module/path.js': { format: 'esm' } } }); ``` SystemJS 0.20 also comes with experimental support for Web Assembly. This can be enabled via: ```javascript SystemJS.config({ wasm: true }); ``` Web Assembly binaries can then be loaded alongside SystemJS modules normally. #### Module format detection When the module format is not set, automatic regular-expression-based detection is used. This module format detection is never completely accurate, but caters well for the majority use cases. The module format detection happens in the following order: * _System.register / System.registerDynamic_ If the source code starts with any number of comments, followed by `System.register` or `System.registerDynamic` as the first line of code. * _ES modules_ The source is only detected as an ES module if it contains explicit module syntax - valid `import` or `export` statements. This detection does not do comment removal, so comments in the code containing valid ES module syntax will trigger this detection. * _AMD modules_ The presence of a valid AMD `define` statement in the code, with all forms of the define statement supported. * _CommonJS modules_ The presence of `require(...)` or `exports` / `module.exports` assigments. * _Global_ This is the fallback module format after all the above fail. When Web Assembly is enabled, Web Assembly modules are automatically detected from their binary header. This is the same detection process that will likely be implemented in browsers. > Note that ES6 modules are detected via the presence of `import` and `export` module syntax and no other features at all. This is because the transpilation applies to the module format specifically, not the language. #### Inter-Format Dependencies Any module type can be loaded from any other type with full support. When loading CommonJS, AMD or Global modules from within ES6, the full module is available at the `default` export which can be loaded with the default import syntax. For convenience, named exports are also auto-populated but may not be correctly bound as expected, so use these carefully. ./app/es6-loading-commonjs: ```javascript // entire underscore instance import _ from './underscore.js'; // unbound named export import {map} from './underscore.js'; ``` ### ES Modules ES6 modules are automatically transpiled as they are loaded, using the loader [transpiler option](config-api.md#transpiler) set. A transpiler plugin must be configured for this to work. Circular references and bindings are implemented to the ES6 specification. The `__moduleName` local variable is also available, pending clarification of the module meta in the WhatWG loader spec. This provides the fully normalized name of the current module which can be useful for dynamically loading modules relative to the current module via: ```javascript SystemJS.import('./local-module', __moduleName); ``` In due course this will be entirely replaced by the contextual loader once this has been specified. _ES modules are loaded via XHR making it incompatible with `scriptLoad: true`. ES modules should always be built for production to avoid transpiler costs, making this a development-only feature._ ### CommonJS * The `module`, `exports`, `require`, `global`, `GLOBAL`, `__dirname` and `__filename` variables are all declared in scope. * `module.id` is set. * `require.resolve` is provided. When executing CommonJS any global `define` is temporarily removed from the global object, before being reverted after synchronous module execution has completed. This ensures that any UMD patterns trigger the CommonJS path. For comprehensive handling of NodeJS modules, a conversion process is needed to make them SystemJS-compatible, such as the one used by jspm. _CommonJS is loaded via XHR making it incompatible with `scriptLoad: true` unless pre-compiling with SystemJS Builder._ Note that CommonJS modules on npm, loaded as CommonJS may well not load correctly through SystemJS. This is because SystemJS does not implement the NodeJS loading algorithm. If you want to load NodeJS modules through SystemJS you can use `import nodeModule from '@node/node-module-name'`, but this should only be used when absolutely necessary as it stops code from being universal, and makes it only compatible with NodeJS. ### AMD * AMD support includes all AMD structural variations including the [CommonJS wrapper form](http://requirejs.org/docs/api.html#cjsmodule). * The special `module`, `exports`, and `require` module names are handled at the AMD format level and are not defined in the primary loader registry. `module.uri` and `module.id` are provided with `module.config` as a no-op. * Named defines are supported and will write directly into the loader registry. * A single named define will write into the loader registry but also be treated as the value of the module loaded if the names do not match. This enables loading a module containing `define('jquery', ...`. * Contextual dynamic requires are fully supported (`define(function(require) { require(['./dynamic/require'], callback) })`) When executing AMD, the global `module`, `exports` are temporarily removed, and the global `define` and `require` are set to the SystemJS AMD functions. _By default AMD modules are loaded via `