golang/go · error · Error

Go.run: WebAssembly.Instance expected

Error message

Go.run: WebAssembly.Instance expected

What it means

Thrown by Go.run(instance) in wasm_exec.js when the argument is not an instance of WebAssembly.Instance. The Go driver expects the already-instantiated wasm module so it can read exports.mem, exports.run, and the syscall shim. Passing the wrong shape (the bytes, the module, the promise) makes every subsequent memory access invalid, so it is rejected up front.

Source

Thrown at lib/wasm/wasm_exec.js:481

							this.mem.setUint8(sp + 48, 0);
							return;
						}
						const toCopy = src.subarray(0, dst.length);
						dst.set(toCopy);
						setInt64(sp + 40, toCopy.length);
						this.mem.setUint8(sp + 48, 1);
					},

					"debug": (value) => {
						console.log(value);
					},
				}
			};
		}

		async run(instance) {
			if (!(instance instanceof WebAssembly.Instance)) {
				throw new Error("Go.run: WebAssembly.Instance expected");
			}
			this._inst = instance;
			this.mem = new DataView(this._inst.exports.mem.buffer);
			this._values = [ // JS values that Go currently has references to, indexed by reference id
				NaN,
				0,
				null,
				true,
				false,
				globalThis,
				this,
			];
			this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
			this._ids = new Map([ // mapping from JS values to reference ids
				[0, 1],
				[null, 2],
				[true, 3],
				[false, 4],

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Instantiate first, then pass the .instance property: const { instance } = await WebAssembly.instantiate(bytes, go.importObject); await go.run(instance);
  2. If using instantiateStreaming: const { instance } = await WebAssembly.instantiateStreaming(fetch('main.wasm'), go.importObject); await go.run(instance);
  3. Verify the value before calling run: if (!(instance instanceof WebAssembly.Instance)) throw new TypeError('expected Instance'); to fail with a clearer message.

Example fix

// before
const module = await WebAssembly.compile(bytes);
await go.run(module); // throws: Go.run: WebAssembly.Instance expected

// after
const { instance } = await WebAssembly.instantiate(bytes, go.importObject);
await go.run(instance);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(instance instanceof WebAssembly.Instance)) {
  throw new TypeError(`go.run expects WebAssembly.Instance, got ${instance && instance.constructor && instance.constructor.name}`);
}

Type guard

const isWasmInstance = (v) => v instanceof WebAssembly.Instance;

Prevention

When it happens

Trigger: Passing a WebAssembly.Module to go.run instead of the instantiated WebAssembly.Instance; passing the result of WebAssembly.instantiateStreaming's resolved value's .instance incorrectly (e.g., passing the result object instead of result.instance); passing a plain object or null.

Common situations: Confusing the two-step instantiate (Module then Instance) pipeline; destructuring WebAssembly.instantiate result incorrectly; copying example code that was written for WebAssembly.instantiateStreaming vs WebAssembly.instantiate.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/857c9e21d9e96f37. Report an issue: GitHub.