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
- Stringify numbers and coerce: String(n).
- Filter undefined/null out of the args array before sending.
- Prefer the typed command wrappers (client.set, client.hSet, ...) which coerce arguments for you.
- 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
- Prefer typed command methods (client.set, client.hSet) over sendCommand; they coerce for you.
- Always String() numbers when building raw args.
- Filter undefined out of optional-arg arrays before sending.
- Convert Uint8Array to Buffer explicitly.
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
- Cannot split : key region does not align with keystep
- Cannot split : key region overruns the arguments
- empty keyValuePairs Argument
- empty toSet Argument
- All statistics values must be non-negative
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)