hexojs/hexo · error · TypeError

name must be a string!

Error message

name must be a string!

What it means

Thrown by Locals.get when 'name' is not a string. Hexo locals are memoized computed values keyed by name; the cache.apply call requires a string key, so a non-string name is rejected before lookup.

Source

Thrown at lib/hexo/locals.ts:13

import { Cache } from 'hexo-util';

class Locals {
  public cache: InstanceType<typeof Cache>;
  public getters: Record<string, () => any>;

  constructor() {
    this.cache = new Cache();
    this.getters = {};
  }

  get(name: string): any {
    if (typeof name !== 'string') throw new TypeError('name must be a string!');

    return this.cache.apply(name, () => {
      const getter = this.getters[name];
      if (!getter) return;

      return getter();
    });
  }

  set(name: string, value: any): this {
    if (typeof name !== 'string') throw new TypeError('name must be a string!');
    if (value == null) throw new TypeError('value is required!');

    const getter = typeof value === 'function' ? value : () => value;

    this.getters[name] = getter;
    this.cache.del(name);

View on GitHub (pinned to 059cb17494)

Solutions

  1. Ensure the name argument is a string before calling get.
  2. When iterating keys, pass the key (string), not the value.
  3. Validate dynamic name sources with typeof checks.

Example fix

// before
hexo.locals.get(maybeName);
// after
if (typeof maybeName === 'string') hexo.locals.get(maybeName);
Defensive patterns

Strategy: validation

Validate before calling

if (typeof name !== 'string') throw new Error('locals.get name must be a string');
hexo.locals.get(name);

Type guard

const isStringName = (n: unknown): n is string => typeof n === 'string';

Prevention

When it happens

Trigger: Calling hexo.locals.get(undefined), get(null), get(123), or get(someObject) where name is not a string.

Common situations: A plugin/theme iterates over object keys but passes the value instead of the key, or a variable meant to hold a name is uninitialized.

Related errors


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