不能将 jsonwebtoken 与最新版本的 React 一起使用

cant use jsonwebtoken with latest version of react

TL:DR: 将 jsonwebtoken 替换为 jwt-decode

我在将现有的 React 应用程序升级到最新版本时遇到了一些问题。每次我都会收到以下错误

Can't resolve 'buffer' in '/home/user/projects/trash/tokentest/test/node_modules/buffer-equal-constant-time'

Can't resolve 'stream' in '/home/user/projects/trash/tokentest/test/node_modules/buffer-equal-constant-time'

Can't resolve 'util' in '/home/user/projects/trash/tokentest/test/node_modules/buffer-equal-constant-time'

Can't resolve 'crypto' in '/home/user/projects/trash/tokentest/test/node_modules/buffer-equal-constant-time'

我可以通过安装 buffer streamutil 包来摆脱除加密之外的所有这些。
我尝试安装 cryptocrypto-browserifycrypto-js.

我确实发现我可以通过从项目中删除 jsonwebtoken 来让它工作。但此后它不再完全可用,因为它需要用户身份验证。

为了测试,我创建了一个全新的 create-react-app 项目。它开箱即用。但是,一旦我安装并导入 jsonwebtoken,我就会再次遇到完全相同的错误。这意味着即使在一个完全干净的项目上,也不能使用 jsonwebtoken

有办法解决这个问题吗?因为我想升级我的项目并使用jsonwebtoken。

jsonwebtoken 是一个 Node.js 模块,您之前在 React 应用程序中使用它依赖于 Node.js std 模块的 polyfill。这很可能是 a) 依赖于不是 maintained anymore 的慢速 js 加密,并且缺乏与 Node 的加密的功能奇偶校验和 b) 大大增加了你的 js 包大小。更新 React 可能意味着更新不再默认使用有问题的加密 polyfill 的 webpack。

最好依赖为浏览器或“通用”制作的模块,您可以在 jwt.io under "JavaScript". Out of the ones listed there, jose 上列出的模块中发现此类模块使用纯可用的 Web API。