mermaid-js/mermaid · error
Layout data is required
Error message
Layout data is required
What it means
Thrown by validateLayoutData when the data argument is falsy (null/undefined). This is the first check in the public validator; it exists so callers can pre-flight a LayoutData object before paying for the layout pass. It is a synchronous throw, not a promise rejection.
Source
Thrown at packages/mermaid-layout-tidy-tree/src/layout.ts:637
points,
sourceSection: sourceNode?.section,
targetSection: targetNode?.section,
sourceWidth: sourceNode?.width,
sourceHeight: sourceNode?.height,
targetWidth: targetNode?.width,
targetHeight: targetNode?.height,
};
});
}
/**
* Validate layout data structure
* @param data - The data to validate
* @returns True if data is valid, throws error otherwise
*/
export function validateLayoutData(data: LayoutData): boolean {
if (!data) {
throw new Error('Layout data is required');
}
if (!data.config) {
throw new Error('Configuration is required in layout data');
}
if (!Array.isArray(data.nodes)) {
throw new Error('Nodes array is required in layout data');
}
if (!Array.isArray(data.edges)) {
throw new Error('Edges array is required in layout data');
}
return true;
}
View on GitHub (pinned to d93e9c88c0)
Solutions
- Initialize LayoutData with empty arrays and a config object rather than leaving it null.
- Add a null check at the call site and skip validation/layout when data is absent.
- Trace the producer of the data argument and ensure it always returns an object.
Example fix
// before
validateLayoutData(maybeData);
// after
if (!maybeData) {
throw new TypeError('LayoutData producer returned nothing');
}
validateLayoutData(maybeData); Defensive patterns
Strategy: type-guard
Validate before calling
if (data == null) {
throw new TypeError('LayoutData is required');
}
validateLayoutData(data); Type guard
function isLayoutData(data: unknown): data is object {
return data != null && typeof data === 'object';
} Prevention
- Type the parameter as LayoutData (non-nullable) at every producer.
- Default LayoutData to an object literal at construction: { nodes: [], edges: [], config: {} }.
When it happens
Trigger: Calling validateLayoutData(null), validateLayoutData(undefined), or passing a variable that was never assigned a LayoutData object.
Common situations: A parser returned null/undefined for a diagram that failed to produce layout data, an async fetch of graph data resolved to nothing, or an uninitialized field on an object was forwarded straight into the validator.
Related errors
- No nodes found in layout data
- Configuration is required in layout data
- Nodes array is required in layout data
- Edges array is required in layout data
- An align directive requires at least two members; got ${hint
AI-assisted analysis of mermaid-js/mermaid@d93e9c88c0 (2026-08-12).
Data as JSON: /api/errors/b0a33fe6ffc9ede9.
Report an issue: GitHub.