Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing decode function from jsonwebtoken causes error #954

Open
michaelmendoza opened this issue Jan 4, 2024 · 5 comments
Open

Importing decode function from jsonwebtoken causes error #954

michaelmendoza opened this issue Jan 4, 2024 · 5 comments

Comments

@michaelmendoza
Copy link

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

@Qolzam
Copy link

Qolzam commented Jan 17, 2024

Hi @michaelmendoza ,
Any solution?

@michaelmendoza
Copy link
Author

michaelmendoza commented Jan 17, 2024

@Qolzam I just used a different library: jwt-decode. Ideally, they would just fix the issue in node-jsonwebtoken. But both libraries are by the same organization.

@ScriptSipper
Copy link

Also having the same issue on version 9.0.2

@phil-w
Copy link

phil-w commented Jun 29, 2024

Yeah I had this, in my case it's caused by me trying to run it in the browser.
See this

Run it on a node.js server - not a browser - and it's fine.

Maybe:

  1. say at the top of the docs in coloured letters that this is it's server only
    Which is where I'll use it, but not where I'm testing it.
  2. add in some instructions on how to polyfil the browser?

@zhushenhang
Copy link

same problem.
Have you solved it yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants