winstonjs/winston · error · Error

Cannot make set from type other than Array of string element

Error message

Cannot make set from type other than Array of string elements

What it means

_stringArrayToSet in the Console transport builds a lookup set from an options array (e.g. handleExceptions/level-related string arrays). It throws this Error when the value passed is not an Array at all (this occurrence, line 113). Winston throws it eagerly in the transport constructor so a misconfigured option fails fast instead of later at log time.

Source

Thrown at lib/winston/transports/console.js:113

    }
  }

  /**
   * Returns a Set-like object with strArray's elements as keys (each with the
   * value true).
   * @param {Array} strArray - Array of Set-elements as strings.
   * @param {?string} [errMsg] - Custom error message thrown on invalid input.
   * @returns {Object} - TODO: add return description.
   * @private
   */
  _stringArrayToSet(strArray, errMsg) {
    if (!strArray) return {};

    errMsg =
      errMsg || 'Cannot make set from type other than Array of string elements';

    if (!Array.isArray(strArray)) {
      throw new Error(errMsg);
    }

    return strArray.reduce((set, el) => {
      if (typeof el !== 'string') {
        throw new Error(errMsg);
      }
      set[el] = true;

      return set;
    }, {});
  }
};

View on GitHub (pinned to ff0b79de85)

Solutions

  1. Pass an actual array of strings for the option, e.g. handleExceptions: ['uncaughtException'] instead of a bare string.
  2. If the value comes from config/env, split/parse it into an array before constructing the transport.
  3. Wrap the transport construction in try/catch and log the offending options object to identify which key is wrong.

Example fix

// before
new winston.transports.Console({ handleExceptions: 'uncaughtException' });
// after
new winston.transports.Console({ handleExceptions: ['uncaughtException'] });
Defensive patterns

Strategy: validation

Validate before calling

function assertStringArray(v, name) {
  if (!Array.isArray(v) || v.some(el => typeof el !== 'string')) {
    throw new TypeError(`${name} must be an Array of strings`);
  }
}
// assertStringArray(opts.handleExceptions, 'handleExceptions');

Type guard

const isStringArray = (v) => Array.isArray(v) && v.every(el => typeof el === 'string');

Try / catch

try {
  transport = new winston.transports.Console(opts);
} catch (err) {
  if (err.message.includes('Cannot make set')) {
    console.error('Bad array option in Console transport config:', opts);
  }
  throw err;
}

Prevention

When it happens

Trigger: new winston.transports.Console({ handleExceptions: <non-array> }) or any option routed through _stringArrayToSet given a string, object, or other non-array value instead of an array of strings (e.g. handleExceptions: 'error' instead of ['error']).

Common situations: Copying config from old winston v2 docs where these options had different shapes; passing a single string instead of wrapping it in an array; loading options from JSON/env where an array becomes a comma-separated string; typos like levels: 'info' passed where an array is expected.

Related errors


AI-assisted analysis of winstonjs/winston@ff0b79de85 (2026-08-31). Data as JSON: /api/errors/376778a8c048a022. Report an issue: GitHub.