ianstormtaylor/slate · error · Error

Cannot get the next node from the root node!

Error message

Cannot get the next node from the root node!

What it means

Editor.next() returns the node after a given location; when 'at' is the root path itself (the empty path []), there is nothing after the root, so Slate throws. The check is explicit because the generic span-based search would otherwise produce nonsense.

Source

Thrown at packages/slate/src/editor/next.ts:21

export const next: EditorInterface['next'] = (editor, options = {}) => {
  const { mode = 'lowest', voids = false } = options
  let { match, at = editor.selection } = options

  if (!at) {
    return
  }

  const pointAfterLocation = Editor.after(editor, at, { voids })

  if (!pointAfterLocation) return

  const [, to] = Editor.last(editor, [])

  const span: Span = [pointAfterLocation.path, to]

  if (Location.isPath(at) && at.length === 0) {
    throw new Error(`Cannot get the next node from the root node!`)
  }

  if (match == null) {
    if (Location.isPath(at)) {
      const [parent] = Editor.parent(editor, at)
      match = n => parent.children.includes(n)
    } else {
      match = () => true
    }
  }

  const [next] = Editor.nodes(editor, { at: span, match, mode, voids })
  return next
}

View on GitHub (pinned to 72a37c701e)

Solutions

  1. Guard before calling: skip when the path is empty (Path.isPath(at) && at.length === 0)
  2. If iterating children of the root, use Editor.nodes with a mode/match, or iterate editor.children directly
  3. Check the at value's derivation — it usually should be a child path like [0], not []

Example fix

// before
const next = Editor.next(editor, { at: path }) // path can be []

// after
const next = path.length === 0 ? null : Editor.next(editor, { at: path })
Defensive patterns

Strategy: type-guard

Validate before calling

const at = somePath
if (!(Path.isPath(at) && at.length === 0)) {
  const next = Editor.next(editor, { at })
}

Type guard

const isNonRootPath = (p) => Array.isArray(p) && p.length > 0

Prevention

When it happens

Trigger: Calling Editor.next(editor, { at: [] }) or Editor.next(editor) when editor.selection or the default location resolves to the root path; passing a path variable that is an empty array (e.g. from Path.parent on a depth-1 path).

Common situations: Walking the tree in a loop and reaching the root; passing Node.path(editor, node) of the editor itself; default 'at' resolving to [] when selection is null and code assumes a deeper path.

Related errors


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