Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Importing `decode` function from jsonwebtoken causes error

Open
#954 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
javascript, vite
Domain
web-dev

Research direction

Reproduce the import in the reported Svelte 4.0.5/Vite 4.4.2 setup and Chrome. Start with node_modules/safe-buffer/index.js and follow the stack through jws/lib/sign-stream.js, jsonwebtoken/decode.js, and jsonwebtoken/index.js; done means importing decode no longer throws the reported browser error.

Written by the indexing model from the issue text.

Description

Description

Provide a clear and concise description of the issue, including what you expected to happen.

Importing decode function from jsonwebtoken causes error. I am unable to import the 'decode' function as normal.

I suspect that this is an issue with vite and svelte. Seems the package safe-buffer, that jsonwebtoken is dependant on, is doing something vite doesn't like saying Module externalized for browser compatibility meaning it is likely using a Node.js module that is not polyfilled in and the vite doesn't accept. That is just my guess. (See vite info on issue here. )

If you make node-jsonwebtoken work with strict mode with all it's dependencies, this may fix this probelm.

Reproduction

In my code I used the following:

import { decode, sign } from 'jsonwebtoken';

I get the following errors:

app.js:28 TypeError: Cannot read properties of undefined (reading 'from')
    at node_modules/safe-buffer/index.js (jsonwebtoken.js?v=1203475a:28:17)
    at __require (chunk-34KZWS7D.js?v=08cf2519:5:50)
    at node_modules/jws/lib/sign-stream.js (jsonwebtoken.js?v=1203475a:591:19)
    at __require (chunk-34KZWS7D.js?v=08cf2519:5:50)
    at node_modules/jws/index.js (jsonwebtoken.js?v=1203475a:771:22)
    at __require (chunk-34KZWS7D.js?v=08cf2519:5:50)
    at node_modules/jsonwebtoken/decode.js (jsonwebtoken.js?v=1203475a:804:15)
    at __require (chunk-34KZWS7D.js?v=08cf2519:5:50)
    at node_modules/jsonwebtoken/index.js (jsonwebtoken.js?v=1203475a:4397:15)
    at __require (chunk-34KZWS7D.js?v=08cf2519:5:50)
Screenshot 2024-01-04 at 14 30 17

Looking into the chrome debugger and stopping the code before the error it seems that it has to do with the package safe-buffer that jsonwebtoken is dependant on.

Screenshot 2024-01-04 at 14 29 56
Environment

Using svelte (v4.0.5) with vite (v4.4.2) on macOS and test with Chrome browser (v120).

Dominant language
JavaScript
Stars
18.2k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from auth0/node-jsonwebtoken

All issues in auth0/node-jsonwebtoken

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.