{"record":{"id":"3fba03e4b055e577","repo":"ianstormtaylor/slate","slug":"cannot-apply-an-insert-node-operation-at-path","errorCode":null,"errorMessage":"Cannot apply an \"insert_node\" operation at path [${path}] because the destination is past the end of the node.","messagePattern":"Cannot apply an \"insert_node\" operation at path \\[(.+?)\\] because the destination is past the end of the node\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/slate/src/interfaces/transforms/general.ts","lineNumber":61,"sourceCode":"   * Transform the editor by an operation.\n   */\n  transform: (editor: Editor, op: Operation) => void\n}\n\n// eslint-disable-next-line no-redeclare\nexport const GeneralTransforms: GeneralTransforms = {\n  transform(editor: Editor, op: Operation): void {\n    let transformSelection = false\n\n    switch (op.type) {\n      case 'insert_node': {\n        const { path, node } = op\n\n        modifyChildren(editor, Path.parent(path), children => {\n          const index = path[path.length - 1]\n\n          if (index > children.length) {\n            throw new Error(\n              `Cannot apply an \"insert_node\" operation at path [${path}] because the destination is past the end of the node.`\n            )\n          }\n\n          return insertChildren(children, index, node)\n        })\n\n        transformSelection = true\n        break\n      }\n\n      case 'insert_text': {\n        const { path, offset, text } = op\n        if (text.length === 0) break\n\n        modifyLeaf(editor, path, node => {\n          const before = node.text.slice(0, offset)\n          const after = node.text.slice(offset)","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/ianstormtaylor/slate/blob/72a37c701e5da4bb13a305d416d9c070d19d3a45/packages/slate/src/interfaces/transforms/general.ts#L43-L79","documentation":"When applying an insert_node operation, Slate inserts the node into the parent's children at the operation's final index. It throws when that index exceeds the parent's current children length, since there would be no contiguous position to insert into.","triggerScenarios":"Applying { type: 'insert_node', path: [0, 5] } when node [0] only has 2 children; usually from hand-crafted operations, replaying/transforming remote operations (collaboration), or applying operations against a different document state than they were generated for.","commonSituations":"OT/collaboration where operations arrive out of order; serializing operations, rebasing them, then applying to a diverged doc; off-by-one path math after deletions; applying queued operations after the editor was reset.","solutions":["Ensure operations are applied in order against the same document state they were computed from; recompute the insertion index from the live document before applying.","Clamp the index: use Math.min(index, parentChildren.length) when generating the operation.","For collaboration, use a proper OT library or Slate's operation-transforming helpers rather than raw apply."],"exampleFix":"// before\nEditor.apply(editor, { type: 'insert_node', path: [0, 5], node }) // parent has 2 children\n\n// after\nconst parent = Node.get(editor, [0])\nconst index = Math.min(5, parent.children.length)\nEditor.apply(editor, { type: 'insert_node', path: [0, index], node })","handlingStrategy":"validation","validationCode":"const parentPath = Path.parent(op.path)\nconst parent = Node.get(editor, parentPath)\nconst index = op.path[op.path.length - 1]\nif (index <= parent.children.length) {\n  Editor.apply(editor, op)\n}","typeGuard":"function isValidInsertPath(editor: Editor, path: Path): boolean {\n  const parent = Node.get(editor, Path.parent(path))\n  const index = path[path.length - 1]\n  return index >= 0 && index <= parent.children.length\n}","tryCatchPattern":"try {\n  Editor.apply(editor, op)\n} catch (e) {\n  if (e instanceof Error && e.message.includes('insert_node')) {\n    // skip or re-derive the index from the current document\n  } else throw e\n}","preventionTips":["Apply operations in generation order against the same doc state.","Clamp computed insertion indices to parent.children.length.","Use OT transforms for remote/collaborative operations instead of raw apply."],"tags":["slate","insert-node","operation","index-out-of-range"],"backgroundTag":"operation-apply-failed","analyzedSha":"72a37c701e5da4bb13a305d416d9c070d19d3a45","analyzedAt":"2026-08-27T23:04:51.145Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}