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

v9: decode does not work with import * in TypeScript #875

Open
bluepichu opened this issue Jan 6, 2023 · 6 comments
Open

v9: decode does not work with import * in TypeScript #875

bluepichu opened this issue Jan 6, 2023 · 6 comments

Comments

@bluepichu
Copy link

Description

If you import * as jwt from "jsonwebtoken", then you can't call jwt.decode. It works fine if you use a default import though (import jwt from "jsonwebtoken").

Related to #866, and probably also #870.

The root cause of this is that import * in TS gets translated in such a way that only enumerable properties are copied from the module. Since decode is non-enumerable it is not properly copied.

Reproduction

import * as jwt from "jsonwebtoken";

jwt.decode("abc"); // TypeError: jwt.decode is not a function

Environment

Version 9.0.0 on Node 16.19.0 with TS 4.6.2

@swapnil-sudhir
Copy link

Any solution for this so far ? @bluepichu

@lloydjatkinson
Copy link

import { decode } from 'jsonwebtoken';

console.log(
    decode(
        'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2NzMyNzcwNTMsImV4cCI6MTcwNDgxMzA1MywiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJFbWFpbCI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJSb2xlIjpbIk1hbmFnZXIiLCJQcm9qZWN0IEFkbWluaXN0cmF0b3IiXX0.cN6HdDt31tosRDffE6UIgDGy3lgjFW9j9uO-VCd8S7U'
    )
);

antgamdia added a commit to vmware-tanzu/kubeapps that referenced this issue Jan 10, 2023
@rpuls
Copy link

rpuls commented Jan 10, 2023

I am having an issue that I think is related. Long story short, I am getting: TypeError: Cannot redefine property: decode when trying to stub decode with sinon.stub - but no issue stubbing the verify function.

I require jwt like this: const jwt = require('jsonwebtoken'); and my stack does not allow me to use import

I posted the issue on OS with some more detail:
https://stackoverflow.com/questions/75071217/jsonwebtoken-9-0-0-getting-error-typeerror-cannot-redefine-property-decode

@bluepichu
Copy link
Author

@rpuls I think your issue is the same as #870 (which has a temporary workaround and an open fix)

jakelacey2012 added a commit to jakelacey2012/node-jsonwebtoken that referenced this issue Feb 13, 2023
jakelacey2012 added a commit that referenced this issue Apr 3, 2023
* refactor: revert 15a1bc4 to to solve #875, #870, and possibly also #876
@rocket2023
Copy link

Version 9.0.0 on Node 16.19.0 and node 18.x with TS 4.x
jwt.sign(...); // TypeError: jwt.sign is not a function

@lloydjatkinson
Copy link

@rocket2023 #875 (comment)

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