{"record":{"id":"055925866e9548e9","repo":"ianstormtaylor/slate","slug":"cannot-get-the-previous-node-from-the-root-node","errorCode":null,"errorMessage":"Cannot get the previous node from the root node!","messagePattern":"Cannot get the previous node from the root node!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/slate/src/editor/previous.ts","lineNumber":25,"sourceCode":"\n  if (!at) {\n    return\n  }\n\n  const pointBeforeLocation = Editor.before(editor, at, { voids })\n\n  if (!pointBeforeLocation) {\n    return\n  }\n\n  const [, to] = Editor.first(editor, [])\n\n  // The search location is from the start of the document to the path of\n  // the point before the location passed in\n  const span: Span = [pointBeforeLocation.path, to]\n\n  if (Location.isPath(at) && at.length === 0) {\n    throw new Error(`Cannot get the previous node from the root node!`)\n  }\n\n  if (match == null) {\n    if (Location.isPath(at)) {\n      const [parent] = Editor.parent(editor, at)\n      match = n => parent.children.includes(n)\n    } else {\n      match = () => true\n    }\n  }\n\n  const [previous] = Editor.nodes(editor, {\n    reverse: true,\n    at: span,\n    match,\n    mode,\n    voids,\n  })","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/ianstormtaylor/slate/blob/72a37c701e5da4bb13a305d416d9c070d19d3a45/packages/slate/src/editor/previous.ts#L7-L43","documentation":"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.","triggerScenarios":"Calling Editor.previous(editor, { at: [] }) or when the default location resolves to the root path; passing a computed path that happens to be empty.","commonSituations":"Tree-walking loops that call previous until exhausted without a root guard; passing the editor node's own path from Node.path(editor, editor).","solutions":["Guard the call: skip when the path is empty","When walking backwards, terminate the loop when the path length is 0 or use Editor.nodes with reverse: true instead"],"exampleFix":"// before\nconst prev = Editor.previous(editor, { at: path }) // path can be []\n\n// after\nconst prev = path.length === 0 ? null : Editor.previous(editor, { at: path })","handlingStrategy":"type-guard","validationCode":"const at = somePath\nif (!(Path.isPath(at) && at.length === 0)) {\n  const prev = Editor.previous(editor, { at })\n}","typeGuard":"const isNonRootPath = (p) => Array.isArray(p) && p.length > 0","tryCatchPattern":null,"preventionTips":["Terminate backward walks at the root","Use Editor.nodes({ reverse: true }) instead of manual previous loops"],"tags":["slate","editor-api","traversal","root-node","path"],"backgroundTag":"invalid-editor-location","analyzedSha":"72a37c701e5da4bb13a305d416d9c070d19d3a45","analyzedAt":"2026-08-27T23:04:51.145Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}