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

  1. Coerce primitives first: crypto.hash('sha256', String(value))
  2. Encode structured data: crypto.hash('sha256', JSON.stringify(obj))
  3. 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

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


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/3b1cb929a8a40642. Report an issue: GitHub.