Compile Motoko source code using mops build to create IC canisters.
Example of how to reference this recipe in an icp.yaml file:
canisters:
- name: backend
recipe:
type: "@dfinity/motoko@<version>"
configuration:
shrink: trueReplace
<version>with a release version (e.g.v5.0.0). See available versions.
The canister must also be defined in mops.toml. The key in the [canisters] section must match the canister name in icp.yaml:
[toolchain]
moc = "1.9.0"
[canisters]
backend = "src/main.mo"Compiler flags, per-canister args, and the Candid file are all configured in mops.toml. See the mops documentation for the full [canisters] schema.
| Parameter | Type | Required | Description | Default |
|---|---|---|---|---|
| metadata | array | No | Custom wasm metadata entries. Each takes name, value, and an optional visibility of public or private (omitted means private) |
[] |
| shrink | boolean | No | Remove unused functions and debug info to reduce file size | false |
| compress | boolean | No | Gzip compress the WASM file | false |
mops(Motoko package manager) — manages the toolchain, dependencies, and canister buildic-wasm(included with icp-cli installation)
Note: If you followed the icp-cli installation guide, both
mopsandic-wasmare already installed.
If mops is not installed, see: https://mops.one/docs/install
# icp.yaml
canisters:
- name: hello-world
recipe:
type: "@dfinity/motoko@<version>"# mops.toml
[toolchain]
moc = "1.9.0"
[canisters]
hello-world = "src/main.mo"# icp.yaml
canisters:
- name: backend
recipe:
type: "@dfinity/motoko@<version>"
configuration:
shrink: true
compress: true
metadata:
- name: "canister:type"
value: "backend"
- name: "build:commit"
value: "a1b2c3d"
visibility: public# mops.toml
[toolchain]
moc = "1.9.0"
[dependencies]
core = "2.5.0"
[moc]
args = ["--default-persistent-actors"]
[canisters.backend]
main = "src/main.mo"
candid = "backend.did"When this recipe is executed:
- Checks if
mopsandic-wasmare installed - Runs
mops build <name>which compiles the canister using the toolchain, dependencies, and compiler flags defined inmops.toml - Copies the built WASM to the icp-cli output path
- Injects compiler version metadata (
moc:version) - Injects template type metadata (
template:type=motoko) - Injects any custom metadata specified in the configuration
- Optionally removes unused functions if
shrinkis enabled - Optionally gzip compresses the WASM file if
compressis enabled
A typical Motoko project structure:
my-project/
├── src/
│ ├── main.mo # Entry point
│ ├── types.mo # Type definitions
│ └── utils.mo # Utility functions
├── mops.toml # Toolchain, dependencies, canister config
└── icp.yaml # Build configuration
The main, candid, and args recipe parameters have been removed. Move them to mops.toml:
Before (icp.yaml) |
After (mops.toml) |
|---|---|
main: src/main.mo |
[canisters.backend] main = "src/main.mo" |
candid: backend.did |
[canisters.backend] candid = "backend.did" |
args: --default-persistent-actors |
[canisters.backend] args = ["--default-persistent-actors"] |
The canister name in icp.yaml is used automatically — no additional recipe configuration is needed.
The [canisters] section is missing from mops.toml, or the canister name does not match the name parameter in icp.yaml. Ensure the key in [canisters] exactly matches the name value in the recipe configuration.
Install the Motoko compiler via mops: run mops install in your project directory. The toolchain version is set in mops.toml under [toolchain].
Check your Motoko syntax and ensure all imports resolve. Run mops check for detailed diagnostics.
- Rust Recipe - For building Rust canisters
- Pre-built Recipe - For using pre-compiled WASM files
- Asset Canister Recipe - For frontend assets canister
See the release history for changelogs, version updates, and breaking changes.