jsonwebtoken throws 'jwt must be provided' in an Express route
JsonWebTokenError: jwt must be providedShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
the first log line prints undefinedThe Authorization header is missing or read with the wrong nameFix - 2
the second log line prints undefined while the browser shows the cookieThe token is in a cookie, but the cookie is not parsedFix - 3
the field is undefined or an empty string in the logThe token is read from req.body or req.query under another nameFix - 4
the log shows a value, but a different variable reaches verifyYour own code passes undefined into verifyFix
- 1
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/me2Fix
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/meConfirmed by expressjs.com
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-parser2Fix
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
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
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
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
Security
jsonwebtoken rejects a token: invalid signature, jwt expired or jwt malformed
JsonWebTokenError: invalid signature
Each message points at a different step.
5 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fix