Running Components
You can "run" a component by calling one of its exports. In some cases, this requires a custom host. For "command" components, though, you can use the wasmtime
command line. This can be a convenient tool for testing components and exploring the component model. Other runtimes are also available - see the "Runtimes" section of the sidebar for more info.
A "command" component is one that exports the
wasi:cli/run
interface, and imports only interfaces listed in thewasi:cli/command
world.
You must use a recent version of wasmtime
(v14.0.0
or greater), as earlier releases of the wasmtime
command line do not include component model support.
To run your component, run:
wasmtime run <path-to-wasm-file>
Running components with custom exports
If you're writing a library-style component - that is, one that exports a custom API - then you can run it in wasmtime
by writing a "command" component that imports and invokes your custom API. By composing the command and the library, you can exercise the library in wasmtime
.
-
Write your library component. The component's world (
.wit
file) must export an interface. (Do not export functions directly, only interfaces.) See the language support guide for how to implement an export. -
Build your library component to a
.wasm
file. -
Write your command component. The component's world (
.wit
file) must import the interface exported from the library. Write the command to call the library's API. See the language support guide for how to call an imported interface. -
Build your command component to a
.wasm
file. You will not be able to run this inwasmtime
yet, as its imports are not yet satisfied. -
Compose your command component with your library component by running
wac plug <path/to/command.wasm> --plug <path/to/library.wasm> -o main.wasm
. -
Run the composed component using
wasmtime run main.wasm
See Composing Components for more details.