BookStackApp/BookStack · error · Error
Expected to find TableElement in DOM
Error message
Expected to find TableElement in DOM
What it means
TableObserver.trackTable runs an editor update that fetches the table's DOM element via editor.getElementByKey(this.tableNodeKey) to start observing it. If the element is not present in the DOM at that moment, it throws 'Expected to find TableElement in DOM'. The observer cannot function without the element it watches.
Source
Thrown at resources/js/wysiwyg/lexical/table/LexicalTableObserver.ts:139
if (
nodeName === 'TABLE' ||
nodeName === 'TBODY' ||
nodeName === 'THEAD' ||
nodeName === 'TR'
) {
gridNeedsRedraw = true;
break;
}
}
if (!gridNeedsRedraw) {
return;
}
const tableElement = this.editor.getElementByKey(this.tableNodeKey);
if (!tableElement) {
throw new Error('Expected to find TableElement in DOM');
}
this.table = getTable(tableElement);
});
});
this.editor.update(() => {
const tableElement = this.editor.getElementByKey(this.tableNodeKey);
if (!tableElement) {
throw new Error('Expected to find TableElement in DOM');
}
this.table = getTable(tableElement);
observer.observe(tableElement, {
attributes: true,
childList: true,
subtree: true,
});View on GitHub (pinned to 18f8469a1c)
Solutions
- Defer observer/selection creation until after the table is rendered (e.g. in a useEffect or command listener post-mount).
- Check editor.getElementByKey(tableNode.getKey()) for null before constructing a TableObserver.
- Ensure the editor root is mounted in the document before initializing table selection state.
- If the table may be deleted, guard lifecycle so observers are not created for detached nodes.
Example fix
// before const observer = new TableObserver(editor, tableNodeKey, ...); // throws if unmounted // after if (!editor.getElementByKey(tableNodeKey)) return; const observer = new TableObserver(editor, tableNodeKey, ...);
Defensive patterns
Strategy: validation
Validate before calling
if (!editor.getElementByKey(tableNodeKey)) {
// defer observer creation until rendered
return;
}
const observer = new TableObserver(editor, tableNodeKey, ...); Try / catch
try {
new TableObserver(editor, tableNodeKey, ...);
} catch (e) {
if (e instanceof Error && e.message === 'Expected to find TableElement in DOM') {
retryWhenMounted(() => new TableObserver(editor, tableNodeKey, ...));
} else throw e;
} Prevention
- Create TableObservers only after the editor root is mounted and content flushed
- Avoid creating observers for nodes that may be deleted concurrently
- In tests, assert the table element exists in the DOM before selection setup
When it happens
Trigger: Constructing a TableObserver (via the table selection plugin) for a table node whose DOM element has not yet been rendered, was removed, or belongs to a headless/unmounted editor.
Common situations: Programmatically creating table selections before the table renders (e.g. in initial editor state setup); tables removed by concurrent updates while an observer starts; test environments where the editor content is not flushed to the DOM.
Related errors
- Table Element Not Found
- Expected TableNode.
- ${method} is not supported in headless mode
- To use $generateHtmlFromNodes in headless mode please initia
- Cell not found in table.
AI-assisted analysis of BookStackApp/BookStack@18f8469a1c (2026-09-02).
Data as JSON: /api/errors/7e870e71edc3f703.
Report an issue: GitHub.