auth0 / node-jsonwebtoken

JsonWebToken implementation for node.js http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html
MIT License
17.5k stars 1.21k forks source link

Importing `decode` function from jsonwebtoken causes error #954

Open michaelmendoza opened 6 months ago

michaelmendoza commented 6 months ago

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 commented 5 months ago

Hi @michaelmendoza , Any solution?

michaelmendoza commented 5 months ago

@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 commented 5 months ago

Also having the same issue on version 9.0.2

phil-w commented 1 week ago

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?