mermaid-js/mermaid · error · Error

No nodes found in layout data

Error message

No nodes found in layout data

What it means

Fourth guard in validateLayoutData: `data.nodes` is missing or is not an array. The layout needs a list of nodes to position; without one cytoscape has nothing to lay out. Thrown at layout.ts:69. Note an empty array [] passes this check (it only rejects undefined/non-array).

Source

Thrown at packages/mermaid/src/rendering-util/layout-algorithms/cose-bilkent/layout.ts:69

 * 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 (!data.rootNode) {
    throw new Error('Root node is required');
  }

  if (!data.nodes || !Array.isArray(data.nodes)) {
    throw new Error('No nodes found 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

  1. Ensure the diagram-db layer always emits data.nodes as an array (even if empty).
  2. If clustering removes all leaf nodes, either retain at least the cluster nodes or skip layout for that case.
  3. Validate with Array.isArray(data.nodes) before calling the layout and short-circuit if absent.
  4. Use the standard render pipeline which guarantees the array shape.

Example fix

// before
{ config, rootNode, edges, layoutAlgorithm: 'cose-bilkent' } // nodes missing
// after
{ config, rootNode, nodes: [...], edges, layoutAlgorithm: 'cose-bilkent' }
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(data?.nodes)) throw new Error('LayoutData.nodes must be an array');

Type guard

function hasNodesArray(d: any): d is { nodes: unknown[] } { return Array.isArray(d?.nodes); }

Try / catch

try { validateLayoutData(data); } catch (e) { if (/No nodes found/.test(String(e))) { data.nodes = []; validateLayoutData(data); } else throw e; }

Prevention

When it happens

Trigger: A LayoutData where nodes was never populated (e.g. a diagram whose parser produced zero nodes and the field was left undefined), or where nodes was set to a non-array (an object/Map). The check is `!data.nodes || !Array.isArray(data.nodes)`.

Common situations: An empty or fully-clustered diagram that yields no concrete nodes, a transform that assigns a Map instead of an array, or a mock fixture missing the field. Also if a cluster-only graph collapses all nodes into subgraphs leaving nodes empty/undefined.

Related errors


AI-assisted analysis of mermaid-js/mermaid@d93e9c88c0 (2026-08-12). Data as JSON: /api/errors/a1cc042391489a36. Report an issue: GitHub.