denoland/deno · error · TypeError
ERR_INVALID_ARG_TYPE
ERR_INVALID_ARG_TYPE
Error message
The "input" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received ${actual} What it means
crypto.hash(algorithm, data, outputEncodingOrOptions) is the one-shot hashing helper. data must be a string or an ArrayBufferView (Buffer, TypedArray, DataView); anything else — numbers, null, booleans, plain objects, BigInts — throws ERR_INVALID_ARG_TYPE for 'input' before hashing.
Source
Thrown at ext/node/polyfills/crypto.ts:144
"crypto.Hmac constructor is deprecated.",
"DEP0181",
);
function getRandomValues(typedArray) {
return webcrypto.getRandomValues(typedArray);
}
function hash(
algorithm: string,
data: BinaryLike,
outputEncodingOrOptions: BinaryToTextEncoding | {
outputEncoding?: BinaryToTextEncoding;
outputLength?: number;
} = "hex",
) {
validateString(algorithm, "algorithm");
if (typeof data !== "string" && !isArrayBufferView(data)) {
throw new ERR_INVALID_ARG_TYPE("input", [
"Buffer",
"TypedArray",
"DataView",
"string",
], data);
}
let outputEncoding: string;
let outputLength: number | undefined;
if (typeof outputEncodingOrOptions === "object") {
outputEncoding = outputEncodingOrOptions.outputEncoding ?? "hex";
outputLength = outputEncodingOrOptions.outputLength;
} else {
outputEncoding = outputEncodingOrOptions;
}
let normalized = outputEncoding;View on GitHub (pinned to 89f33cbef2)
Solutions
- Coerce primitives first: crypto.hash('sha256', String(value))
- Encode structured data: crypto.hash('sha256', JSON.stringify(obj))
- Use Buffer.from(...) to produce a view for binary-ish input
Example fix
// before
crypto.hash("sha256", 123456);
// after
crypto.hash("sha256", String(123456)); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof data !== "string" && !ArrayBuffer.isView(data)) {
throw new TypeError(`crypto.hash data must be string or ArrayBufferView, got ${typeof data}`);
} Type guard
const isHashable = (d) => typeof d === "string" || ArrayBuffer.isView(d);
Try / catch
try { digest = crypto.hash(algo, data); }
catch (e) {
if (e.code === "ERR_INVALID_ARG_TYPE" && e.message.includes('"input"')) {
digest = crypto.hash(algo, String(data));
} else throw e;
} Prevention
- Stringify values from JSON before hashing
- Keep a project helper that always returns string | Uint8Array for hash inputs
- Remember null is not hashable even though typeof null === 'object'
When it happens
Trigger: crypto.hash('sha256', 123); crypto.hash('sha256', null); crypto.hash('sha256', { data: 'x' }). typeof null === 'object' but it is not an ArrayBufferView, so null also throws.
Common situations: Hashing values that came from JSON.parse (numbers) without coercion; passing Blob/FormData or other structured objects; code ported from libraries that relied on implicit toString for hashing.
Related errors
- ERR_INVALID_ARG_VALUE
- If "keyFormat" is specified, it must be "pem": received "${k
- ${name} must be 'an integer' but was ${value}
- Output length ${outputLength} is invalid for ${algoLower}, w
- The bench name can't be empty
AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16).
Data as JSON: /api/errors/3b1cb929a8a40642.
Report an issue: GitHub.