denoland/deno · error · ERR_INVALID_ARG_VALUE

ERR_INVALID_ARG_VALUE

ERR_INVALID_ARG_VALUE

Error message

The property 'options.privateKey' is invalid. Received ${options.privateKey}

What it means

The options object passed to crypto.diffieHellman() must contain a truthy privateKey; a missing, undefined, or null value throws ERR_INVALID_ARG_VALUE for 'options.privateKey'. This is a presence check only — an incorrect type is caught later when the key handle is consumed.

Source

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

}

function diffieHellman(
  options: {
    privateKey: KeyObject;
    publicKey: KeyObject;
  },
  callback?: (err: Error | null, secret?: Buffer) => void,
): Buffer | void {
  if (callback !== undefined && typeof callback !== "function") {
    throw new ERR_INVALID_ARG_TYPE("callback", "function", callback);
  }
  if (
    typeof options !== "object" || options === null || ArrayIsArray(options)
  ) {
    throw new ERR_INVALID_ARG_TYPE("options", "object", options);
  }
  if (!options.privateKey) {
    throw new ERR_INVALID_ARG_VALUE("options.privateKey", options.privateKey);
  }
  if (!options.publicKey) {
    throw new ERR_INVALID_ARG_VALUE("options.publicKey", options.publicKey);
  }

  if (callback) {
    try {
      const secret = statelessDH(options.privateKey, options.publicKey);
      callback(null, secret);
    } catch (err) {
      callback(err as Error);
    }
    return;
  }

  return statelessDH(options.privateKey, options.publicKey);
}

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Supply options.privateKey as a KeyObject from crypto.createPrivateKey()
  2. Fail fast at startup when required key material cannot be loaded, instead of passing undefined through
  3. Add a runtime assertion or schema check on the options object before calling

Example fix

// before
diffieHellman({ publicKey: theirPub });

// after
diffieHellman({ privateKey: myPriv, publicKey: theirPub });
Defensive patterns

Strategy: validation

Validate before calling

if (!options?.privateKey) {
  throw new Error('privateKey missing: load it with crypto.createPrivateKey before DH');
}
crypto.diffieHellman(options);

Type guard

const hasPrivateKey = (o: unknown): o is { privateKey: crypto.KeyObject } =>
  typeof o === 'object' && o !== null && (o as any).privateKey?.type === 'private';

Prevention

When it happens

Trigger: diffieHellman({ publicKey }) with privateKey omitted; destructuring typos ({ privateKey: pub }); optional-chained config where the private key failed to load and is undefined.

Common situations: Env-driven key loading where a missing env var silently yields undefined; mocks/stubs in tests that build partial options; copy-paste between the privateKey and publicKey fields.

Related errors


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