denoland/deno · error · NodeTypeError

ERR_INVALID_ARG_TYPE

ERR_INVALID_ARG_TYPE

Error message

The "sizeOrKey" argument must be one of type number or string or an instance of ArrayBuffer, Buffer, TypedArray, or DataView

What it means

The DiffieHellman constructor (diffiehellman.ts:129) requires sizeOrKey to be a number (prime bit length), a string (encoded prime), or binary data (Buffer/TypedArray/DataView/ArrayBuffer). Anything else throws ERR_INVALID_ARG_TYPE enumerating all accepted types. BigInt is the frequent offender because it is none of the above.

Source

Thrown at ext/node/polyfills/internal/crypto/diffiehellman.ts:129

  #primeLength: number;
  #generator: Buffer;
  #privateKey: Buffer;
  #publicKey: Buffer;
  #publicKeyNeedsUpdate = false;

  constructor(
    sizeOrKey: number | string | ArrayBufferView,
    keyEncoding?: unknown,
    generator?: unknown,
    genEncoding?: unknown,
  ) {
    if (
      typeof sizeOrKey !== "number" &&
      typeof sizeOrKey !== "string" &&
      !isArrayBufferView(sizeOrKey) &&
      !isAnyArrayBuffer(sizeOrKey)
    ) {
      throw new ERR_INVALID_ARG_TYPE(
        "sizeOrKey",
        [
          "number",
          "string",
          "ArrayBuffer",
          "Buffer",
          "TypedArray",
          "DataView",
        ],
        sizeOrKey,
      );
    }

    if (typeof sizeOrKey === "number") {
      validateInt32(sizeOrKey, "sizeOrKey");
    }

    if (

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Convert BigInt primes to Buffer: Buffer.from(prime.toString(16), 'hex')
  2. Pass the bit length as a number, or the prime as a Buffer
  3. Destructure params objects before constructing: new DiffieHellman(params.prime, 2)

Example fix

// before
new DiffieHellman(bigIntPrime); // BigInt rejected

// after
new DiffieHellman(Buffer.from(bigIntPrime.toString(16), 'hex'), 2);
Defensive patterns

Strategy: type-guard

Validate before calling

function isValidSizeOrKey(v) {
  return typeof v === 'number' || typeof v === 'string' || ArrayBuffer.isView(v) || v instanceof ArrayBuffer;
}

Type guard

const isSizeOrKey = (v) => typeof v === 'number' || typeof v === 'string' || ArrayBuffer.isView(v) || v instanceof ArrayBuffer;

Prevention

When it happens

Trigger: new DiffieHellman(BigInt(prime)) or a BigInt produced by a big-number library; passing null/undefined/plain objects; passing a DH params object ({ prime }) instead of its field.

Common situations: Primes arriving from bignum/BN.js computations as BigInt; JSON config objects passed wholesale; argument order mix-ups where keyEncoding lands in sizeOrKey's slot.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/e7c49124eab8a274. Report an issue: GitHub.