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
Coordinates not in base64url
commonJWK x/y/d must be base64url without padding (RFC 7518). Values in standard base64 (with + / =), hex strings, or decimal integers do not decode to the expected 32 bytes for P-256.
Truncated coordinate (the leading-zero bug)
commonSome encoders strip leading zero bytes from big integers, producing a 31-byte coordinate. JWK requires fixed-length coordinates (32 bytes for P-256); a short x or y makes the JWK invalid or moves the point off the curve.
crv does not match the key material
occasionalA JWK stamped crv: "P-256" carrying secp256k1 or P-384 coordinates (after manual editing or template copy-paste) cannot describe a point on the declared curve.
How to fix it
- 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.
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.
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.