ianstormtaylor/slate · error · Error

Cannot get the leaf node at path [${path}] because it refers

Error message

Cannot get the leaf node at path [${path}] because it refers to a non-leaf node: ${Scrubber.stringify(
          node
        )}

What it means

Mirror of the element case: modifyLeaf throws when the path resolves to a non-Text node (an Element) but a text leaf is required. Text-level operations like setting text properties or splitting text need an actual Text node at the path.

Source

Thrown at packages/slate/src/utils/modify.ts:91

        )
      }

      return { ...node, children: f(node.children) }
    })
  }
}

/**
 * Replace a leaf, replacing all ancestors
 */
export const modifyLeaf = (
  root: Ancestor,
  path: Path,
  f: (leaf: Text) => Text
) =>
  modifyDescendant(root, path, node => {
    if (!Node.isText(node)) {
      throw new Error(
        `Cannot get the leaf node at path [${path}] because it refers to a non-leaf node: ${Scrubber.stringify(
          node
        )}`
      )
    }

    return f(node)
  })

View on GitHub (pinned to 72a37c701e)

Solutions

  1. Verify Node.isText(Editor.node(editor, path)[0]) before text-level operations
  2. Descend to the first text leaf with Editor.leaf(editor, path) or Node.first to get a valid leaf path
  3. Adjust path computation to target the actual leaf (usually one level deeper)

Example fix

// before
Transforms.setNodes(editor, { bold: true }, { at: [0] }) // [0] is an element

// after
const entry = Editor.leaf(editor, [0]) // first text leaf
Transforms.setNodes(editor, { bold: true }, { at: entry[1] })
Defensive patterns

Strategy: type-guard

Validate before calling

const [leaf, leafPath] = Editor.leaf(editor, path)
Transforms.setNodes(editor, { bold: true }, { at: leafPath })

Type guard

const isTextAt = (editor: Editor, p: Path): boolean =>
  Text.isText(Editor.node(editor, p)[0])

Prevention

When it happens

Trigger: Text-level transforms or manual modifyLeaf calls with a path pointing at an Element (e.g. [0] when intending [0, 0]); ops assuming a leaf exists where an element sits.

Common situations: Off-by-one path depth in custom text transforms; documents where an inline element occupies the expected leaf slot; deserialized documents with unexpected nesting.

Related errors


AI-assisted analysis of ianstormtaylor/slate@72a37c701e (2026-08-27). Data as JSON: /api/errors/f5dd40ded9cad7f2. Report an issue: GitHub.