如何在 Webpack 5 中为 jsonwebtoken 填充缓冲区

2024-04-24

我正在升级到 Webpack 5,并且 jsonwebtoken 包存在问题(https://github.com/auth0/node-jsonwebtoken https://github.com/auth0/node-jsonwebtoken)需要缓冲区(在https://github.com/auth0/node-jsonwebtoken/blob/master/sign.js#L91 https://github.com/auth0/node-jsonwebtoken/blob/master/sign.js#L91) 由于 Nodejs 函数不包含 Webpack 5 polyfill,因此我尝试使用该函数sign从 jsonwebtoken 它抛出以下错误:

message: "Buffer is not defined"
stack: "ReferenceError: Buffer is not defined↵    
at module.exports (webpack-internal:///./node_modules/jsonwebtoken/sign.js:91:26)↵ 

解决我安装的问题https://github.com/feross/buffer https://github.com/feross/buffer with

npm install buffer

在我的 webpack 配置中我添加了

 resolve: {
    fallback: {
      "Buffer": require.resolve('buffer/'),
    }

or

 resolve: {
    fallback: {
      "buffer": require.resolve('buffer/'),
    }

我也尝试过

 resolve: {
    fallback: {
      "buffer": require.resolve('buffer/').Buffer,
    }

但最后一个会产生 Webpack 架构错误:

 configuration.resolve.fallback['Buffer'] should be one of these:
      [non-empty string, ...] | false | non-empty string
      -> New request.
      Details:
       * configuration.resolve.fallback['Buffer'] should be an array:
         [non-empty string, ...]
         -> Multiple alternative requests.
       * configuration.resolve.fallback['Buffer'] should be false.
         -> Ignore request (replace with empty module).
       * configuration.resolve.fallback['Buffer'] should be a non-empty string.
         -> New request.
    at validate (/home/ant1/packcity/front-pmd/node_modules/webpack/node_modules/schema-utils/dist/validate.js:104:11)

尽管我进行了尝试,但它不起作用并且错误仍然存​​在。

有人成功在与 Webpack 捆绑在一起的应用程序中添加了 Buffer 的 polyfill 吗? 任何帮助将非常感激。


我刚刚通过添加解决了我的问题

    new webpack.ProvidePlugin({
      Buffer: ['buffer', 'Buffer'],
    }),

正如这里所建议的https://github.com/ipfs/js-ipfs/issues/3369#issuecomment-721975183 https://github.com/ipfs/js-ipfs/issues/3369#issuecomment-721975183

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Webpack 5 中为 jsonwebtoken 填充缓冲区 的相关文章

随机推荐