redis/node-redis · error · TypeError

"arguments[ ]" must be of type "string | Buffer", got…

Error message

"arguments[${i}]" must be of type "string | Buffer", got ${typeof arg} instead.

What it means

The RESP encoder iterates command args and only accepts values of type string or Buffer. Anything else (number, boolean, undefined, null, plain object, array, Uint8Array) throws a TypeError naming the offending index. Numbers and other primitives must be stringified by the caller before reaching the encoder.

Solutions

  1. Stringify numbers and coerce: String(n).
  2. Filter undefined/null out of the args array before sending.
  3. Prefer the typed command wrappers (client.set, client.hSet, ...) which coerce arguments for you.
  4. Convert Uint8Array to Buffer: Buffer.from(uint8).

Example fix

// before
client.sendCommand(['SET', 'k', 42, undefined]);

// after
client.sendCommand(['SET', 'k', String(42)]);
Defensive patterns

Strategy: type-guard

Validate before calling

function sanitizeArgs(args) {
  const out = [];
  for (let i = 0; i < args.length; i++) {
    const a = args[i];
    if (a == null) continue; // drop undefined/null
    if (typeof a === 'string' || Buffer.isBuffer(a)) {
      out.push(a);
    } else if (typeof a === 'number' || typeof a === 'bigint' || typeof a === 'boolean') {
      out.push(String(a));
    } else if (a instanceof Uint8Array) {
      out.push(Buffer.from(a));
    } else {
      throw new TypeError(`arguments[${i}] unsupported type ${typeof a}`);
    }
  }
  return out;
}

client.sendCommand(sanitizeArgs(rawArgs));

Type guard

function isValidArg(a) {
  return typeof a === 'string' || Buffer.isBuffer(a);
}

function allValidArgs(args) {
  return Array.isArray(args) && args.every(isValidArg);
}

Prevention

When it happens

Trigger: Calling client.sendCommand([name, ...args]) with a non-string/non-Buffer arg; building args manually and leaving an optional slot as undefined; passing a Date, JSON object, or number raw; multi.addCommand(['CMD', 42, undefined]).

Common situations: Forgetting String(n) when assembling raw args; an optional flag left undefined; passing Uint8Array instead of Buffer; a helper returning undefined that flows into args; serializing an object with JSON.stringify forgotten.

Related errors


AI-assisted analysis of redis/node-redis@90fd0652bc (2026-08-11). Data as JSON: /api/errors/3fdd5bd1b3b5842d. Report an issue: GitHub.

Appendix: source

Thrown at packages/client/lib/RESP/encoder.ts:21

const CRLF = '\r\n';

export default function encodeCommand(args: ReadonlyArray<RedisArgument>): ReadonlyArray<RedisArgument> {
  const toWrite: Array<RedisArgument> = [];

  let strings = '*' + args.length + CRLF;

  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (typeof arg === 'string') {
      strings += '$' + Buffer.byteLength(arg) + CRLF + arg + CRLF;
    } else if (arg instanceof Buffer) {
      toWrite.push(
        strings + '$' + arg.length.toString() + CRLF,
        arg
      );
      strings = CRLF;
    } else {
      throw new TypeError(`"arguments[${i}]" must be of type "string | Buffer", got ${typeof arg} instead.`);
    }
  }

  toWrite.push(strings);

  return toWrite;
}

View on GitHub (pinned to 90fd0652bc)