WebAssembly.Module
A WebAssembly.Module object contains stateless WebAssembly code that has already been compiled by the browser — this can be efficiently shared with Workers, and instantiated multiple times.
A **`WebAssembly.Module`** object contains stateless WebAssembly code that has already been compiled by the browser — this can be efficiently [shared with Workers](/docs/Web/API/Worker/postMessage), and instantiated multiple times.
`WebAssembly.Module` is a subclass of the hidden AbstractModuleSource class, in environments that support `AbstractModuleSource`.
> [!NOTE]
> The `WebAssembly.Module` object is unrelated to the [`Module`](https://emscripten.org/docs/api_reference/module.html) object used in Emscripten.
## Constructor
- [`WebAssembly.Module()`](/docs/WebAssembly/Reference/JavaScript_interface/Module/Module)
- : Creates a new `Module` object.
## Static methods
- [`WebAssembly.Module.customSections()`](/docs/WebAssembly/Reference/JavaScript_interface/Module/customSections_static)
- : Given a `Module` and string, returns a copy of the contents of all custom sections in the module with the given string name.
- [`WebAssembly.Module.exports()`](/docs/WebAssembly/Reference/JavaScript_interface/Module/exports_static)
- : Given a `Module`, returns an array containing descriptions of all the declared exports.
- [`WebAssembly.Module.imports()`](/docs/WebAssembly/Reference/JavaScript_interface/Module/imports_static)
- : Given a `Module`, returns an array containing descriptions of all the declared imports.
## Examples
### Sending a compiled module to a worker
The following example compiles the loaded `simple.wasm` byte code using the [`WebAssembly.compileStreaming()`](/docs/WebAssembly/Reference/JavaScript_interface/compileStreaming_static) method and sends the resulting `Module` instance to a [worker](/docs/Web/API/Web_Workers_API) using postMessage().
See the `index-compile.html` [source code](https://github.com/mdn/webassembly-examples/blob/main/js-api-examples/index-compile.html) or [view it live](https://mdn.github.io/webassembly-examples/js-api-examples/index-compile.html).
```js
const worker = new Worker("wasm_worker.js");
WebAssembly.compileStreaming(fetch("simple.wasm")).then((mod) =>
worker.postMessage(mod),
);
```
The worker function [`wasm_worker.js`](https://github.com/mdn/webassembly-examples/blob/main/js-api-examples/wasm_worker.js) defines an import object for the module to use. The function then sets up an event handler to receive the module from the main thread. When the module is received, we create an instance from it using the [`WebAssembly.instantiate()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiate_static) method and invoke an exported function from inside it.
```js
const importObject = {
my_namespace: {
imported_func(arg) {
console.log(arg);
},
},
};
onmessage = (e) => {
console.log("module received from main thread");
const mod = e.data;
WebAssembly.instantiate(mod, importObject).then((instance) => {
instance.exports.exported_func();
});
};
```
### Obtaining the module using `import source`
In environments that support source phase imports, you can obtain a `WebAssembly.Module` object using the [`import source`](/docs/Web/JavaScript/Reference/Statements/import/source) statement or the [`import.source()`](/docs/Web/JavaScript/Reference/Operators/import/source) operator. Both avoid the need to call `fetch()` or `WebAssembly.compileStreaming()`, relying instead on the default module loader machinery.
```js
import source modSource from "./simple.wasm";
const worker = new Worker("wasm_worker.js");
worker.postMessage(modSource);
```
## Specifications
## Browser compatibility
## See also
- [WebAssembly](/docs/WebAssembly) overview
- [WebAssembly concepts](/docs/WebAssembly/Guides/Concepts)
- [Using the WebAssembly JavaScript API](/docs/WebAssembly/Guides/Using_the_JavaScript_API)
Cite this
"WebAssembly.Module". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/Module