hexojs/hexo · error · TypeError

fn must be a function

Error message

fn must be a function

What it means

Thrown by SyntaxHighlight.register when 'fn' is not a function. Hexo registers named syntax highlighter engines (e.g. 'highlight.js', 'prismjs'); the registered value must be a callable that performs highlighting.

Source

Thrown at lib/extend/syntax_highlight.ts:43

interface StoreFunction {
  (content: string, options: HighlightOptions): string;
  priority?: number;
}

interface Store {
  [key: string]: StoreFunction
}

class SyntaxHighlight {
  public store: Store;

  constructor() {
    this.store = {};
  }

  register(name: string, fn: StoreFunction): void {
    if (typeof fn !== 'function') throw new TypeError('fn must be a function');

    this.store[name] = fn;
  }

  query(name: string): StoreFunction {
    return name && this.store[name];
  }

  exec(name: string, options: HighlightExecArgs): string {
    const fn = this.store[name];

    if (!fn) throw new TypeError(`syntax highlighter ${name} is not registered`);
    const ctx = options.context;
    const args = options.args || [];

    return Reflect.apply(fn, ctx, args);
  }
}

View on GitHub (pinned to 059cb17494)

Solutions

  1. Pass a function as the highlighter implementation.
  2. Verify the imported highlight engine is defined before registering.
  3. Ensure the function signature matches StoreFunction.

Example fix

// before
hexo.extend.syntax_highlight.register('hljs', hljsConfig);
// after
hexo.extend.syntax_highlight.register('hljs', (args) => hljs.highlightAuto(args[0]).value);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof fn !== 'function') throw new Error('highlighter must be a function');
hexo.extend.syntax_highlight.register(name, fn);

Type guard

const isHighlightFn = (x: unknown): x is (...a: any[]) => any => typeof x === 'function';

Prevention

When it happens

Trigger: Calling hexo.extend.syntax_highlight.register(name, undefined), register(name, 'notafn'), or register(name, someObject) where the second argument is not a function.

Common situations: A highlighter plugin passes a configuration object instead of the highlight function, or the imported engine resolved to undefined.

Related errors


AI-assisted analysis of hexojs/hexo@059cb17494 (2026-08-12). Data as JSON: /api/errors/4e7d5c96dc5140d9. Report an issue: GitHub.