facebook/lexical · error

CodeHighlightPlugin: CodeNode or CodeHighlightNode not regis

Error message

CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor

What it means

registerCodeHighlighting() in @lexical/code-prism refuses to run unless the editor knows about both CodeNode and CodeHighlightNode, because the highlighting transforms and decorations can only operate on those node classes. Lexical editors only handle node types explicitly registered at creation, so attempting to highlight code without them means the plugin could encounter nodes it cannot process. The library throws eagerly at registration time instead of failing later during an update.

Source

Thrown at packages/lexical-code-prism/src/CodeHighlighterPrism.ts:436

      $textNodeTransform.bind(null, editor, tokenizer, transformState),
    ),
  );

  return mergeRegister(...registrations);
}

/**
 * Register the Prism tokenizer-driven highlighting on the editor along
 * with the indent / Tab / arrow-key keyboard handlers. This function
 * is provided for legacy code that has not upgraded to using
 * {@link CodePrismExtension}.
 */
export function registerCodeHighlighting(
  editor: LexicalEditor,
  tokenizer: Tokenizer = PrismTokenizer,
): () => void {
  if (!editor.hasNodes([CodeNode, CodeHighlightNode])) {
    throw new Error(
      'CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor',
    );
  }
  return mergeRegister(
    registerHighlightingOnly(editor, tokenizer),
    registerCodeIndentation(editor),
  );
}

export interface CodePrismConfig {
  /**
   * When true, the Prism code highlighter is not registered on the editor.
   * This signal can be flipped at runtime to enable or disable the
   * highlighter, for example to switch between the Prism and Shiki
   * highlighters without rebuilding the editor.
   */
  disabled: boolean;
  tokenizer: Tokenizer;

View on GitHub (pinned to 76a22dcba9)

Solutions

  1. Add CodeNode and CodeHighlightNode (from @lexical/code or @lexical/code-prism) to the editor's nodes array
  2. If using extensions, include the code extension (e.g. CodeHighlightExtension) in buildEditorFromExtensions so nodes are registered automatically
  3. Verify with editor.hasNodes([CodeNode, CodeHighlightNode]) before calling registerCodeHighlighting

Example fix

// before
const editor = createEditor({nodes: [HeadingNode, QuoteNode]});
registerCodeHighlighting(editor); // throws

// after
import {CodeNode, CodeHighlightNode} from '@lexical/code';
const editor = createEditor({nodes: [HeadingNode, QuoteNode, CodeNode, CodeHighlightNode]});
registerCodeHighlighting(editor); // ok
Defensive patterns

Strategy: validation

Validate before calling

import {CodeNode, CodeHighlightNode} from '@lexical/code';
if (!editor.hasNodes([CodeNode, CodeHighlightNode])) {
  throw new Error('Register CodeNode and CodeHighlightNode before enabling code highlighting');
}
registerCodeHighlighting(editor);

Type guard

function canHighlight(editor: LexicalEditor): boolean {
  return editor.hasNodes([CodeNode, CodeHighlightNode]);
}

Try / catch

try {
  const unregister = registerCodeHighlighting(editor);
} catch (e) {
  if (String(e).includes('not registered on editor')) {
    console.error('Add CodeNode and CodeHighlightNode to your editor config', e);
  }
}

Prevention

When it happens

Trigger: Calling registerCodeHighlighting(editor) (or mounting CodeHighlightPlugin, which calls it) on an editor whose initial nodes config omitted CodeNode or CodeHighlightNode.

Common situations: Adding a code block to a rich-text editor that was created with createEditor({nodes: [...]}) or buildEditorFromExtensions without including the code nodes; copying plugin setup from docs into an editor configured with a hand-picked nodes list; switching from the umbrella @lexical/code package to the @lexical/code-prism package and forgetting the nodes are no longer auto-registered.

Related errors


AI-assisted analysis of facebook/lexical@76a22dcba9 (2026-08-31). Data as JSON: /api/errors/1fcead6d57781e3a. Report an issue: GitHub.