Can a Wasm Module Import Another Wasm Module?

WebAssembly (Wasm) modules cannot natively import other Wasm modules directly in standard browser environments without the aid of JavaScript or a bundler. While the WebAssembly specification defines an import section, the actual resolution and linking of these imports are delegated to the host environment. This article explains how Wasm imports currently function, how JavaScript acts as the intermediary link, and the future specifications that will enable direct imports.

The Role of the Host Environment

WebAssembly is designed to be host-independent. A Wasm binary declares what it needs to import (such as functions, memory, tables, or globals) and what it exports, but it does not specify how to locate or retrieve those imports.

In a web browser, the host environment is JavaScript. When you instantiate a WebAssembly module using the WebAssembly.instantiate() or WebAssembly.instantiateStreaming() APIs, you must explicitly provide an imports object. Because of this architecture, a Wasm module cannot directly fetch and link to another .wasm file on its own.

The Current Solution: JavaScript Glue Code

To connect two WebAssembly modules today, you must use JavaScript as a bridge to pass the exports of one module into the imports of another.

The workflow typically follows these steps: 1. Compile or fetch the first WebAssembly module (Module A). 2. Instantiate Module A to access its exported functions. 3. Fetch and compile the second WebAssembly module (Module B). 4. Pass Module A’s exported functions inside the imports object when instantiating Module B.

This manual linking process ensures that Module B can call functions defined in Module A, but the orchestration is entirely handled by JavaScript.

Tooling and Bundlers

Many developers experience what feels like direct Wasm-to-Wasm imports when using modern web bundlers like Webpack, Vite, or Rollup.

If you write code in a language like Rust or C++ and import one module into another, the compiler and your build tools generate the necessary JavaScript glue code behind the scenes. During the build process, the bundler packages the modules and automatically wires the exports of one Wasm binary to the imports of another, simulating a direct connection.

The Future: ES Module Integration

To solve this limitation and allow truly direct imports, the WebAssembly community is developing the WebAssembly ES Module Integration proposal.

Once fully standardized and implemented by browser vendors, this feature will allow WebAssembly modules to be integrated directly into the JavaScript module graph. You will be able to import a WebAssembly module directly inside a JavaScript file, or even import a Wasm module from another Wasm module using standard import syntax:

import { exportedFunction } from './module.wasm';

Under this specification, the browser’s engine will handle the fetching, compilation, and linking of Wasm modules automatically, eliminating the need for manual JavaScript instantiation glue code.