{"record":{"id":"bfcfd4b64bc68794","repo":"GrapesJS/grapesjs","slug":"cannot-add-a-layer-model-to-another-layer-model","errorCode":null,"errorMessage":"Cannot add a layer model to another layer model","messagePattern":"Cannot add a layer model to another layer model","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/utils/sorter/LayerNode.ts","lineNumber":48,"sourceCode":"  /**\n   * Get the parent LayerNode of this component, or null if it has no parent.\n   * @returns The parent LayerNode or null.\n   */\n  getParent(): LayerNode | null {\n    const collection = this.model instanceof Layer ? this.model.collection : null;\n    return collection ? new LayerNode(collection as Layers) : null;\n  }\n\n  /**\n   * Add a child LayerNode at a particular index in the Layers model.\n   * @param node - The LayerNode to add as a child.\n   * @param index - The position to insert the child.\n   * @returns The newly added LayerNode.\n   * @throws Error if trying to add to a Layer (not a Layers).\n   */\n  addChildAt(node: LayerNode, index: number) {\n    if (this.model instanceof Layer) {\n      throw Error('Cannot add a layer model to another layer model');\n    }\n\n    const newModel = this.model.add(node.model, { at: index });\n    return new LayerNode(newModel);\n  }\n\n  /**\n   * Remove a child LayerNode at a specified index in the Layers model.\n   * @param index - The index of the child to remove.\n   * @returns The removed LayerNode.\n   * @throws Error if trying to remove from a Layer (not a Layers).\n   */\n  removeChildAt(index: number) {\n    if (this.model instanceof Layer) {\n      throw Error('Cannot remove a layer model from another layer model');\n    }\n\n    const child = this.model.at(index);","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/GrapesJS/grapesjs/blob/2bdeda85b82b8b9ceae42fd6558cbbcae5ec2d21/packages/core/src/utils/sorter/LayerNode.ts#L30-L66","documentation":"LayerNode wraps either a Layers collection (a container of layers) or a single Layer model. addChildAt performs this.model.add(...), which is only valid on the Layers collection; if the node wraps a Layer, the library throws this error because a layer cannot contain other layers.","triggerScenarios":"Calling addChildAt(node, index) on a LayerNode whose model is a Layer (a leaf) instead of a Layers collection — e.g. dropping a layer onto another layer rather than into the root Layers collection, or an off-by-one level error when walking the layer tree.","commonSituations":"Custom drag-and-drop layer management built on the internal sorter utilities; nesting layers by mistake when the style manager's layer tree is only two levels (Layers -> Layer); tests or scripts that fetch children of a Layer (getChildren returns null) and then assume an insertable parent.","solutions":["Only call addChildAt on nodes whose model is a Layers collection — check with `node.model instanceof Layers` first","Insert into the root Layers collection or the parent collection of the target layer instead of the Layer itself","If building drop logic, use canMove()/getChildren() (returns null for leaf Layers) to reject drops onto leaf nodes before calling addChildAt"],"exampleFix":"// before\nnode.addChildAt(child, 0); // node.model may be a Layer\n// after\nif (node.model instanceof Layers) {\n  node.addChildAt(child, 0);\n}","handlingStrategy":"type-guard","validationCode":"if (!(node.model instanceof Layers)) {\n  throw new Error('addChildAt requires a Layers collection node');\n}","typeGuard":"function isLayersNode(n: LayerNode): n is LayerNode & { model: Layers } {\n  return n.model instanceof Layers;\n}","tryCatchPattern":"try {\n  node.addChildAt(child, index);\n} catch (e) {\n  if (e.message.includes('Cannot add a layer model')) {\n    // route insertion to the parent/root Layers collection instead\n  }\n}","preventionTips":["Check node.model instanceof Layers before any addChildAt call","Treat Layer nodes as leaves: getChildren() returning null means not insertable","Use canMove() before drag-and-drop inserts","Insert new layers into the root Layers collection, never into another Layer"],"tags":["tree","layers","sorter","typescript"],"backgroundTag":"invalid-tree-parent-node","analyzedSha":"2bdeda85b82b8b9ceae42fd6558cbbcae5ec2d21","analyzedAt":"2026-08-30T11:47:52.267Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}