JWT decode returns null in Node? Strip the Bearer prefix
jwt.decode returns null or "the token header and payload are NULL"? Strip "Bearer " from the Authorization header. Code, checks and a decoder test.
TroubleshootingUpdated 5 min read
On this page
Your Node auth middleware logs Token: Bearer eyJ..., and jwt.decode(token, { complete: true }) gives null, or you see:
the token header and payload are NULL
Verification fails every time, even with the right secret.
Why it happens
A JWT is three Base64URL segments separated by dots. The word Bearer and the space after it are part of the HTTP header scheme, not part of the token. When you hand the library Bearer eyJ..., it can't parse the first segment, so decode returns null and verify rejects the token.
Causes, most common first:
- The prefix was never stripped. Your log line may say
Token: Bearer eyJ...;Token:is only your own label, and the value is still the full header. - The client sends a bad value after Bearer.
Bearer undefined,Bearer nullorBearer "eyJ..."(with quotes, often from a JSON-stringified value in browser storage) pass a prefix check and then fail to parse. - The token was cut or reformatted. Copies from terminals or logs can pick up line breaks, trailing spaces or a truncated tail.
- The token isn't in that header. It may arrive in a cookie or query string, or a proxy may drop
Authorization.
Step-by-step fixes
1. Strip the Bearer prefix
The shortest version:
const token = req.headers.authorization.split(' ')[1];It throws if the header is missing (undefined.split) and returns garbage if the header has a different shape. A safer version:
const jwt = require('jsonwebtoken');
function tokenFromHeader(header) {
if (typeof header !== 'string') return null;
const match = header.match(/^Bearer\s+(\S+)$/i);
return match ? match[1] : null;
}
// Express-style middleware
function auth(req, res, next) {
const token = tokenFromHeader(req.headers.authorization);
if (!token) return res.status(401).json({ error: 'Missing bearer token' });
const decoded = jwt.decode(token, { complete: true }); // header + payload, no verification
if (!decoded) return res.status(401).json({ error: 'Malformed token' });
try {
req.user = jwt.verify(token, process.env.JWT_SECRET); // checks the signature
next();
} catch (err) {
res.status(401).json({ error: 'Invalid token' });
}
}- Node lowercases incoming header names, so read
req.headers.authorization. /^Bearer\s+(\S+)$/iaccepts any casing ofBearerand extra spaces, and rejects anything trailing the token.header.replace(/^Bearer /, '')also works, but a header that never had a prefix passes through unchanged and fails later with a less obvious error.
2. Check what actually arrives
Log the value right after stripping it. A real token usually starts with eyJ and has exactly three segments:
console.log(JSON.stringify(token), token && token.split('.').length);JSON.stringify makes stray quotes, spaces and newlines visible. If you see "undefined" or a quoted string, fix the client. Call token.trim() if copies pick up whitespace. If req.headers.authorization is undefined, log req.headers once: the token may be in another header, or a proxy in front of your app may be dropping it.
3. Confirm the token itself parses
Paste the raw token (without Bearer ) into a decoder. If you see a header and a payload, the token is fine and the bug is in your code. If the decoder can't read it either, the token is broken (see step 2).
In Bytepliers this works offline:
- Open JWT Decoder (⌘4).
- Paste into Token.
- Read the Header and Payload JSON. Badges show
algand Expired, Not Yet Valid, Valid or No Expiry, withiat,nbfandexptimes.
4. Decode works but verify fails
Decoding checks nothing; anyone can read a payload. Verification needs the right key. To test a secret by hand, open the Signature pane in JWT Decoder and enter it as Secret, choosing the encoding (UTF-8, Base64 or Hex). You get "Signature verified" or "Invalid signature". For RS or ES tokens, use Public key instead (a PEM public key or certificate, bare Base64 DER, a JWK or a JWK Set). Keys are never saved, and "Remember keys" is off by default.
5. Decode from a script or the terminal
With the bytepliers command-line tool installed (Settings > Integrations > Command-Line Tool > Install…), bytepliers jwt decodes a token's payload and bytepliers jwt --header decodes the header:
pbpaste | bytepliers jwt --headerThis reads the clipboard with the built-in pbpaste and prints the header. Open the app once first to enable the tool; it needs an active trial or license. See the command-line docs.
Cause, symptom, fix
| Cause | Symptom | Fix |
|---|---|---|
| Prefix not stripped | decode returns null | split(' ')[1] or the regex helper |
Bearer undefined | Token is the string undefined | Fix the client; check storage |
| Quoted token | Value starts with "eyJ | Remove JSON.stringify on the client |
| Whitespace or truncation | Fewer than 3 segments | trim(), check the copy |
| Wrong secret | decode works, verify throws | Match secret and encoding on both services |
| Expired token | verify throws an expiry error | Check the Expired badge, issue a new token |
Checklist before you try again
- The value you pass starts with
eyJ, notBearer. - It has exactly three dot-separated segments and no quotes or whitespace.
- You read
req.headers.authorization(lowercase) and handle a missing header. - The same secret, in the same encoding, is used to sign and verify.
- The token isn't expired or not yet valid.
If nothing works
If the token parses in a decoder but your library still returns null or throws, ask the library's maintainers or your team. Never share the secret or a live token.
jwt.decode(token, { complete: true }) returns null in Node, but the token
decodes fine in a separate decoder.
Node version:
jsonwebtoken version:
Token header (alg and typ only, no signature):
How I extract the token:
How I call decode and verify:
Exact error from verify: Common questions
Does jwt.decode check the signature?
No. It only reads the header and payload. Use jwt.verify with the key to check the signature and claims such as expiry.
Is "Bearer" case-sensitive?
The scheme name is case-insensitive in HTTP, so some clients send bearer. The regex above uses the i flag to accept both.
Is it safe to paste a token into a decoder?
Treat production tokens as credentials. An offline decoder keeps the token on your machine. JWT Decoder in Bytepliers doesn't record its input in history unless you turn on "Also record inputs that look like secrets", and its copies are marked concealed for clipboard managers.
Can I check a token without opening the app?
If the token is on your clipboard, press ⌥⌘B (the default Quick Panel shortcut) in any app. Auto-detect recognizes a JWT and shows its payload, alg and status.
Check the token offline
Bytepliers is part of MacSuite and includes JWT Decoder with signature checks. You can try it free for 7 days (as of November 2026). For a walkthrough, see how to decode a JWT token on Mac.