ERR_CRYPTO_INCOMPATIBLE_KEY
Node.jsERRORNotableCryptoHIGH confidence

Crypto key is incompatible with the requested operation

Production Risk

Prevents key agreement or encryption from completing; test thoroughly with the actual key material.

What this means

Thrown when a crypto key is valid but incompatible with the specific algorithm or operation being used. For example, using an EC key with the wrong curve for ECDH, or providing a key that is too short for the requested security level.

Why it happens
  1. 1Using an EC key with a curve not supported by the requested operation
  2. 2Key algorithm does not match the cipher or signing algorithm
  3. 3ECDH key exchange attempted with keys on different curves
How to reproduce

Triggered when the crypto subsystem validates key compatibility with the chosen algorithm.

trigger — this will error
trigger — this will error
const { createECDH } = require('crypto');
const alice = createECDH('prime256v1');
const bob = createECDH('secp384r1'); // different curve
alice.generateKeys();
bob.generateKeys();
alice.computeSecret(bob.getPublicKey()); // throws — curves don't match

expected output

Error [ERR_CRYPTO_INCOMPATIBLE_KEY]: Incompatible key type

Fix

Ensure both parties use the same ECDH curve

WHEN When performing ECDH key exchange

Ensure both parties use the same ECDH curve
const alice = createECDH('prime256v1');
const bob = createECDH('prime256v1'); // same curve
alice.generateKeys();
bob.generateKeys();
const secret = alice.computeSecret(bob.getPublicKey());

Why this works

ECDH requires both parties to use the same elliptic curve for the math to work.

Code examples
Triggerjs
const { createECDH } = require('crypto');
const alice = createECDH('prime256v1');
const bob = createECDH('secp384r1'); // different curve
alice.generateKeys();
bob.generateKeys();
alice.computeSecret(bob.getPublicKey()); // throws — curves don't match  // this triggers ERR_CRYPTO_INCOMPATIBLE_KEY
Handle in try/catchjs
try {
  // operation that may throw ERR_CRYPTO_INCOMPATIBLE_KEY
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_CRYPTO_INCOMPATIBLE_KEY') {
    console.error('ERR_CRYPTO_INCOMPATIBLE_KEY:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_crypto_incompatible_key(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Mix keys from different algorithms or curves in a single operation

Cryptographic operations require key and algorithm compatibility; mixing them produces undefined or erroneous results.

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