ianstormtaylor/slate · error · Error

Cannot get the previous node from the root node!

Error message

Cannot get the previous node from the root node!

What it means

Editor.previous() returns the node before a given location; when 'at' is the root path (empty path []), there is no previous node, so Slate throws. It is the mirror of the 'next' root guard.

Source

Thrown at packages/slate/src/editor/previous.ts:25

  if (!at) {
    return
  }

  const pointBeforeLocation = Editor.before(editor, at, { voids })

  if (!pointBeforeLocation) {
    return
  }

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

  // The search location is from the start of the document to the path of
  // the point before the location passed in
  const span: Span = [pointBeforeLocation.path, to]

  if (Location.isPath(at) && at.length === 0) {
    throw new Error(`Cannot get the previous 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 [previous] = Editor.nodes(editor, {
    reverse: true,
    at: span,
    match,
    mode,
    voids,
  })

View on GitHub (pinned to 72a37c701e)

Solutions

  1. Guard the call: skip when the path is empty
  2. When walking backwards, terminate the loop when the path length is 0 or use Editor.nodes with reverse: true instead

Example fix

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

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

Strategy: type-guard

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Calling Editor.previous(editor, { at: [] }) or when the default location resolves to the root path; passing a computed path that happens to be empty.

Common situations: Tree-walking loops that call previous until exhausted without a root guard; passing the editor node's own path from Node.path(editor, editor).

Related errors


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