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). Hex strings and decimal integers do not decode to the expected 32 bytes for P-256. Node is lenient about one variant: standard base64 with + / and = padding imports without complaint, while browsers reject the same JWK with a DataError — a key that works on the server can still fail in the client.
Truncated or corrupted coordinate
commonA coordinate that lost a character in transit, or had one changed, describes a different number, and the point leaves the curve. The classic leading-zero bug — an encoder that writes 31 bytes because the first byte was zero — is the one truncation Node forgives: the number is unchanged, so the key imports. Browsers reject the same JWK ("should be 32" bytes), which is why it is worth fixing anyway.
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.