jwt-decode import fails in a React project
import jwt_decode from "jwt-decode" no longer worksShort answer
Version 4 of jwt-decode removed the default export. Import the named function jwtDecode instead of a default, and in CommonJS destructure it from require. Check the installed version first, because old tutorials still show the default import.
Fix it fast
1Find out which cause you have
Check which version is installed.
npm ls jwt-decode2Match what you see, then jump to its fix
- 1
Version 4 or newer is installed and the code reads import jwtDecode from 'jwt-decode'A default import against version 4Fix - 2
A CommonJS file uses const jwtDecode = require('jwt-decode')require() used without destructuringFix - 3
InvalidTokenError: token is not a string, or a missing partThe token itself is falsy or malformedFix
- 1
What does the error look like?
import jwt_decode from "jwt-decode" no longer worksInvalidTokenError
Cause 1 of 3
A default import against version 4
1Check
Search the code for the old form.
grep -rn "from .jwt-decode" src/2Fix
Use the named export.
import { jwtDecode } from "jwt-decode";
const payload = jwtDecode(token);3Verify
The build passes and the payload logs.
npm run buildConfirmed by raw.githubusercontent.com
Cause 2 of 3
require() used without destructuring
1Check
Look for require in config or server files.
grep -rn "require(.jwt-decode" .2Fix
Destructure the named export.
const { jwtDecode } = require('jwt-decode');3Verify
Run the file.
node -e "const {jwtDecode}=require('jwt-decode'); console.log(typeof jwtDecode)"Confirmed by github.com
Cause 3 of 3
The token itself is falsy or malformed
1Check
Log the token before decoding. It must be a string with three dot-separated parts.
console.log(typeof token, token?.split('.').length)2Fix
Decode only when a token exists, and catch the error.
try { const p = token ? jwtDecode(token) : null; } catch (e) { /* treat as logged out */ }3Verify
A bad token no longer crashes the page.
// reload with empty storageConfirmed by github.com
How do I stop it coming back?
- Read the changelog when a major version of a dependency changes.
- Remember that decoding does not validate a token; verify it on the server.
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix