ecdsa.com
jose (JavaScript)JWT libraries

Error message

Invalid Compact JWS

jose (JavaScript) — JWSInvalid, code ERR_JWS_INVALID

What it means

jose could not split your input into the three dot-separated segments of compact JWS serialization (header.payload.signature). The string you passed is structurally not a signed token; nothing cryptographic was attempted. It is jose's equivalent of jsonwebtoken's "jwt malformed" — and like that error, it means the problem is upstream of the JWT library, in whatever produced or transported the string.

Why it happens

How to fix it

  1. 1.

    Sanitize and segment-check the input

    Trim, strip the scheme, and assert the segment count before calling jose — you get precise app-level errors instead of a generic parse failure.

    js
    const token = raw.replace(/^Bearer\s+/i, "").trim();
    const segments = token.split(".").length;
    if (segments !== 3) throw new Error(`expected JWS (3 segments), got ${segments}`);
  2. 2.

    Route JWE tokens to decryption

    If you count five segments, the token is encrypted. Use jwtDecrypt with the recipient's private key instead of jwtVerify.

    js
    import { jwtDecrypt } from "jose";
    
    const { payload } = await jwtDecrypt(token, privateKey); // 5-segment JWE
  3. 3.

    Trace where the string was built

    If a sanitized value still fails, log its length and first characters at every hop — client, gateway, storage, handler. A signed JWT's first segment is base64url JSON, so real tokens start with "eyJ" (the encoding of '{"'); the hop where that stops being true is where the corruption happens.

    js
    console.log(token.length, JSON.stringify(token.slice(0, 12)));
    // healthy: 300+ 'eyJhbGciOiJF'

Related errors

← Browse the full signature error database