Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

jwt-decode import fails in a React project

Errorimport jwt_decode from "jwt-decode" no longer works

Short 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.

  • Applies to jwt-decode 4, React 19, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which version is installed.

    npm ls jwt-decode
  2. 2Match what you see, then jump to its fix

    1. 1Version 4 or newer is installed and the code reads import jwtDecode from 'jwt-decode'A default import against version 4Fix
    2. 2A CommonJS file uses const jwtDecode = require('jwt-decode')require() used without destructuringFix
    3. 3InvalidTokenError: token is not a string, or a missing partThe token itself is falsy or malformedFix

What does the error look like?

  • import jwt_decode from "jwt-decode" no longer works
  • InvalidTokenError

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 build

Confirmed by raw.githubusercontent.com

Did this fix it?

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

Did this fix it?

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 storage

Confirmed by github.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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