Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

jsonwebtoken throws 'jwt must be provided' in an Express route

ErrorJsonWebTokenError: jwt must be provided

Short answer

jsonwebtoken throws this when the token string passed to jwt.verify is empty or undefined. The library is fine; your code did not find a token on the request. Log what you pass to verify, then check the three places tokens usually come from: the Authorization header, a cookie, or the body or query.

  • Applies to Express 5, Node.js 22, jsonwebtoken 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact value you give to jwt.verify, just before the call. If it is undefined or an empty string, the problem is on the request side, not in the secret.

    console.log('auth header:', req.get('Authorization'));
    console.log('cookie token:', req.cookies && req.cookies.token);
  2. 2Match what you see, then jump to its fix

    1. 1the first log line prints undefinedThe Authorization header is missing or read with the wrong nameFix
    2. 2the second log line prints undefined while the browser shows the cookieThe token is in a cookie, but the cookie is not parsedFix
    3. 3the field is undefined or an empty string in the logThe token is read from req.body or req.query under another nameFix
    4. 4the log shows a value, but a different variable reaches verifyYour own code passes undefined into verifyFix

What does the error look like?

  • JsonWebTokenError: jwt must be provided

Cause 1 of 4

The Authorization header is missing or read with the wrong name

1Check

req.get() reads a request header and ignores letter case, so a different header name such as x-access-token returns undefined. Also check that the client really sends the header (browser dev tools, Network tab).

curl -i -H "Authorization: Bearer $TOKEN" http://localhost:3000/api/me

2Fix

Read the header the client actually sends, split off the Bearer prefix, and answer 401 yourself when nothing is there instead of letting verify throw.

const header = req.get('Authorization') || '';
const token = header.startsWith('Bearer ') ? header.slice(7) : null;
if (!token) return res.status(401).json({ error: 'Token missing' });
const payload = jwt.verify(token, process.env.JWT_SECRET);

3Verify

A request without the header gets 401, and one with a valid token reaches the route.

curl -i http://localhost:3000/api/me
curl -i -H "Authorization: Bearer $TOKEN" http://localhost:3000/api/me

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 4

The token is in a cookie, but the cookie is not parsed

1Check

Express fills req.cookies only when cookie-parser is loaded; without it the property is not set by Express.

npm ls cookie-parser

2Fix

Install and load cookie-parser before the routes that read the cookie, and check that the client sends the cookie.

npm install cookie-parser

const cookieParser = require('cookie-parser');
app.use(cookieParser());

3Verify

Call the route again from the client and print req.cookies.

console.log(req.cookies);

Confirmed by expressjs.com

Did this fix it?

Cause 3 of 4

The token is read from req.body or req.query under another name

1Check

req.body is undefined until a body-parsing middleware such as express.json() runs, and the values in req.body and req.query come from the client, so a typo in the field name on either side gives undefined.

console.log(req.body);
console.log(req.query);

2Fix

Load express.json() before the route, and use the same field name on the client and the server.

app.use(express.json());

const token = req.body.token;

3Verify

Send the field and see the route run.

curl -i -X POST http://localhost:3000/api/verify -H 'Content-Type: application/json' -d '{"token":"<paste token>"}'

Confirmed by expressjs.com

Did this fix it?

Cause 4 of 4

Your own code passes undefined into verify

1Check

jsonwebtoken throws this message when its token argument is falsy. Search for every jwt.verify call and check the variable it receives, including a token stored in a variable that is only set after login.

grep -rn "jwt.verify" src/

2Fix

Pass the variable you logged, and guard the call so a missing token returns 401.

if (!token) return res.status(401).end();
const payload = jwt.verify(token, secret);

3Verify

The log and the verify call now show the same value.

node -e "require('jsonwebtoken').verify('', 'x', e => console.log(e.message))"

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Put token reading in one small middleware, so only one place can get the header name wrong.
  • Return 401 yourself when the token is missing, and keep jwt.verify inside a try/catch for the other JsonWebTokenError cases.
  • Treat everything from req.body and req.query as untrusted input and validate it.
  • Add a test for a request with no token and one with a valid token.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.