Skip to content

Importing decode function from jsonwebtoken causes error #954

Open
@michaelmendoza

Description

@michaelmendoza

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions