ecdsa.com
Node.js cryptoNode.js crypto

Error message

Invalid JWK EC key

Node.js crypto — TypeError, code ERR_CRYPTO_INVALID_JWK

What it means

createPublicKey/createPrivateKey with format: "jwk" rejected the JWK object: required members are missing, coordinates fail to decode, or the point (x, y) is not actually on the named curve. Node validates the point on import, so a corrupted or truncated coordinate is caught here rather than producing a key that never verifies. The same checks run whether the JWK came from a JWKS endpoint, a config file or another service — the error indicts the object, not the transport.

Why it happens

How to fix it

  1. 1.

    Validate the JWK's shape before importing

    Check members and decoded lengths. For P-256, x and y must each decode to exactly 32 bytes.

    js
    const x = Buffer.from(jwk.x, "base64url");
    const y = Buffer.from(jwk.y, "base64url");
    console.log(jwk.kty, jwk.crv, x.length, y.length); // EC P-256 32 32
  2. 2.

    Left-pad short coordinates

    If a producer stripped leading zeros, restore the fixed length before building the JWK.

    js
    const pad32 = (b) => Buffer.concat([Buffer.alloc(32 - b.length), b]);
    jwk.x = pad32(Buffer.from(jwk.x, "base64url")).toString("base64url");
    jwk.y = pad32(Buffer.from(jwk.y, "base64url")).toString("base64url");
    const key = crypto.createPublicKey({ key: jwk, format: "jwk" });
  3. 3.

    Regenerate from a trusted source

    If coordinates fail validation and you did not build the JWK yourself, re-fetch it from the issuer's JWKS endpoint rather than repairing bytes — a JWK that needs surgery is a JWK you should not trust.

Related errors

← Browse the full signature error database