ianstormtaylor/slate · error · Error

Cannot get the next path of a root path [${path}], because i

Error message

Cannot get the next path of a root path [${path}], because it has no next index.

What it means

Path.next() returns the sibling path immediately after the given one by incrementing the last index. The root path [] has no last index to increment, so Slate throws rather than returning an undefined result. It signals the caller tried to advance past the top level of the tree.

Source

Thrown at packages/slate/src/interfaces/path.ts:327

  levels(path: Path, options: PathLevelsOptions = {}): Path[] {
    const { reverse = false } = options
    const list: Path[] = []

    for (let i = 0; i <= path.length; i++) {
      list.push(path.slice(0, i))
    }

    if (reverse) {
      list.reverse()
    }

    return list
  },

  next(path: Path): Path {
    if (path.length === 0) {
      throw new Error(
        `Cannot get the next path of a root path [${path}], because it has no next index.`
      )
    }

    const last = path[path.length - 1]
    return path.slice(0, -1).concat(last + 1)
  },

  operationCanTransformPath(
    operation: Operation
  ): operation is
    | InsertNodeOperation
    | RemoveNodeOperation
    | MergeNodeOperation
    | SplitNodeOperation
    | MoveNodeOperation {
    switch (operation.type) {
      case 'insert_node':

View on GitHub (pinned to 72a37c701e)

Solutions

  1. Guard before incrementing: if (path.length === 0) break/return — the root has no next sibling.
  2. Bound sibling iteration by the parent's children count: compare Path.next(p) against the number of children of Path.parent(p).
  3. Prefer Slate's built-in traversals (Node.nodes, Editor.nodes) which handle boundaries, over manual next-walking.
  4. In loops that ascend with Path.parent, stop when path.length === 0 before any next() call.

Example fix

// before
let p = startPath
while (Editor.hasPath(editor, p)) { visit(p); p = Path.next(p) }
// throws when startPath's chain reaches []

// after
let p = startPath
while (p.length > 0 && Editor.hasPath(editor, p)) {
  visit(p)
  p = Path.next(p)
}
Defensive patterns

Strategy: validation

Validate before calling

if (path.length === 0) { /* root: no next sibling, stop */ }

Type guard

const hasNextSibling = (p: Path): boolean => p.length > 0

Prevention

When it happens

Trigger: Calling Path.next([]) directly; looping with Path.next in a while-loop that walks siblings until reaching the root; calling Path.next(Path.parent(p)) where p was already length 1 (parent is []).

Common situations: Iteration code like `let p = path; while (true) { ...; p = Path.next(p) }` without a bounds check; converting recursive traversal to iterative next-walking; off-by-one in loops that decrement path length down to zero.

Related errors


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