ERR_CRYPTO_ECDH_INVALID_FORMAT
Node.jsERRORNotableCryptoHIGH confidence

Invalid format for ECDH public key

Production Risk

Low — caught immediately at call site.

What this means

Thrown when an invalid format argument is passed to ecdh.getPublicKey() or ecdh.setPublicKey(). The valid format values are "compressed", "uncompressed", and "hybrid". Any other string causes this error.

Why it happens
  1. 1Passing an unrecognised format string to getPublicKey()
  2. 2Typo in the format argument (e.g. "compress" instead of "compressed")
  3. 3Passing undefined or null as the format argument
How to reproduce

Triggered when getPublicKey() or setPublicKey() receives an invalid format argument.

trigger — this will error
trigger — this will error
const { createECDH } = require('crypto');
const ecdh = createECDH('prime256v1');
ecdh.generateKeys();
ecdh.getPublicKey(null, 'compact'); // 'compact' is not a valid format

expected output

Error [ERR_CRYPTO_ECDH_INVALID_FORMAT]: Invalid ECDH format: compact

Fix

Use a valid format string

WHEN When calling getPublicKey() or setPublicKey()

Use a valid format string
const pub = ecdh.getPublicKey(null, 'compressed');
// or
const pub2 = ecdh.getPublicKey(null, 'uncompressed');

Why this works

Valid format values are "compressed", "uncompressed", and "hybrid"; using one satisfies the validation.

Code examples
Triggerjs
const { createECDH } = require('crypto');
const ecdh = createECDH('prime256v1');
ecdh.generateKeys();
ecdh.getPublicKey(null, 'compact'); // 'compact' is not a valid format  // this triggers ERR_CRYPTO_ECDH_INVALID_FORMAT
Handle in try/catchjs
try {
  // operation that may throw ERR_CRYPTO_ECDH_INVALID_FORMAT
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_CRYPTO_ECDH_INVALID_FORMAT') {
    console.error('ERR_CRYPTO_ECDH_INVALID_FORMAT:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_crypto_ecdh_invalid_format(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Pass arbitrary strings as the format argument

Only three exact string values are accepted; anything else throws.

Same error in other languages
Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors