d2lang/d2 · error · Error
response.error.message
Error message
response.error.message
What it means
In the Node worker (worker.js) compile case, d2.compile (WASM) returns a JSON string; a truthy response.error makes the worker throw Error(response.error.message), which is posted back to the main thread and rejects the caller's promise. This is the Node-side counterpart of the browser compile error and surfaces D2 compilation failures.
Source
Thrown at d2js/js/src/worker.js:34
switch (type) {
case "init":
try {
if (isNode) {
loadScript(data.wasmExecContent);
}
d2 = await initWasm(data.wasm);
currentPort.postMessage({ type: "ready" });
} catch (err) {
currentPort.postMessage({ type: "error", error: err.message });
}
break;
case "compile":
try {
const result = await d2.compile(JSON.stringify(data));
const response = JSON.parse(result);
if (response.error) throw new Error(response.error.message);
currentPort.postMessage({ id, type: "result", data: response.data });
} catch (err) {
currentPort.postMessage({ id, type: "error", error: err.message });
}
break;
case "render":
try {
const result = await d2.render(JSON.stringify(data));
const response = JSON.parse(result);
if (response.error) throw new Error(response.error.message);
const decoded = new TextDecoder().decode(
Uint8Array.from(atob(response.data), (c) => c.charCodeAt(0))
);
currentPort.postMessage({ id, type: "result", data: decoded });
} catch (err) {
currentPort.postMessage({ id, type: "error", error: err.message });
}View on GitHub (pinned to 0d69dca6f5)
Solutions
- Fix the diagram source based on the error message (it includes compiler diagnostics)
- Lint/validate D2 scripts in CI before rendering
- Check d2.version() for compatibility with any newer syntax used
- Wrap compile in try/catch and return a meaningful error to the caller
Example fix
// before
await d2.compile('a -> b ->'); // trailing incomplete connection
// after
await d2.compile('a -> b'); Defensive patterns
Strategy: validation
Validate before calling
if (typeof source !== 'string' || source.trim() === '') {
throw new TypeError('D2 source must be a non-empty string');
}
const result = await d2.compile(source); Type guard
function isCompileInput(input) {
return typeof input === 'string' ||
(input !== null && typeof input === 'object' && !Array.isArray(input));
} Try / catch
try {
const result = await d2.compile(source);
} catch (err) {
// err.message contains the D2 compiler diagnostic from the wasm build
console.error('D2 compile failed (node):', err.message);
throw new SyntaxError('Invalid D2 script: ' + err.message);
} Prevention
- Lint D2 files in CI before server-side compilation
- Return the compiler diagnostic to API consumers instead of a generic 500
- Pin the d2.wasm version; verify syntax compatibility after upgrades
- Add unit tests compiling representative diagrams
When it happens
Trigger: Calling d2.compile() on a D2 instance in Node with a script that fails to compile: syntax errors, invalid references, unknown keywords, or invalid options in the request object.
Common situations: Server-side rendering of user-supplied D2 scripts with syntax errors; CI jobs compiling diagrams checked into a repo with typos; options incompatible with the bundled wasm D2 version.
Related errors
- response.error.message
- response.error.message
- failed to %scompile: %w
- native Markdown SVG does not support HTML element <%s>
- Failed to decompress SourceSansPro-Regular: %v
AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31).
Data as JSON: /api/errors/fc73b369db6f302a.
Report an issue: GitHub.