hexojs/hexo · error · TypeError
fn must be a function
Error message
fn must be a function
What it means
Thrown by Generator.register when no function can be resolved. Hexo generators build the site's route data; the (fn) form requires the single argument to be a function, otherwise an auto-name cannot be assigned. The (name, fn) form also requires fn callable.
Source
Thrown at lib/extend/generator.ts:49
}
list(): Store {
return this.store;
}
get(name: string): StoreFunction {
return this.store[name];
}
register(fn: GeneratorFunction): void
register(name: string, fn: GeneratorFunction): void
register(name: string | GeneratorFunction, fn?: GeneratorFunction): void {
if (!fn) {
if (typeof name === 'function') { // fn
fn = name;
name = `generator-${this.id++}`;
} else {
throw new TypeError('fn must be a function');
}
}
if (fn.length > 1) fn = Promise.promisify(fn);
this.store[name as string] = Promise.method(fn);
}
}
export = Generator;
View on GitHub (pinned to 059cb17494)
Solutions
- Pass a function as the generator (register(fn)) or as the second argument (register(name, fn)).
- Verify the imported generator symbol is defined.
- Ensure you did not pass an options object as the sole argument.
Example fix
// before
hexo.extend.generator.register(genConfig);
// after
hexo.extend.generator.register(locals => { /* return routes */ }); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof fn !== 'function') throw new Error('generator must be a function');
hexo.extend.generator.register(fn); Type guard
const isGeneratorFn = (x: unknown): x is (...a: any[]) => any => typeof x === 'function';
Prevention
- Register generators with the single-function form unless you need a custom name.
- Confirm imported generator modules export a function.
- Type generator registration wrappers to reject non-functions.
When it happens
Trigger: Calling register('non-function-string') with no fn, or register(someObject) where the argument is not a function.
Common situations: A plugin registers a generator but the handler import is undefined, or an object/config was passed instead of a function.
Related errors
- fn must be a function
- fn must be a function
- fn must be a function
- fn must be a function
- name is required
AI-assisted analysis of hexojs/hexo@059cb17494 (2026-08-12).
Data as JSON: /api/errors/7e8483c9a14fde15.
Report an issue: GitHub.