问题描述
我正在升级到Webpack 5,并且软件包jsonwebtoken( https://github.com/auth0/node-jsonwebtoken )需要缓冲区(位于 https://github.com/auth0/node-jsonwebtoken/blob/master/sign.js#L91 )由于Node.js函数不包含Webpack 5 polyfills,因此我尝试使用jsonwebtoken中的函数 sign
,它会引发以下错误:
I am upgrading to Webpack 5 and I have an issue with the package jsonwebtoken (https://github.com/auth0/node-jsonwebtoken) that needs Buffer (at https://github.com/auth0/node-jsonwebtoken/blob/master/sign.js#L91)Since Webpack 5 polyfills are not included for nodejs functions and wen I try to use the function sign
from jsonwebtoken it throws the following error :
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 与
To solve the issue I installed https://github.com/feross/buffer with
npm install buffer
在我的webpack配置中添加
and in my webpack config I added
resolve: {
fallback: {
"Buffer": require.resolve('buffer/'),
}
或
resolve: {
fallback: {
"buffer": require.resolve('buffer/'),
}
我也尝试过
resolve: {
fallback: {
"buffer": require.resolve('buffer/').Buffer,
}
但是最后一个会产生Webpack模式错误:
But this last one produce a Webpack schema error :
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)
尽管进行了试用,但仍然无法正常工作,并且错误仍然存在.
Despite my trials it is not working and the error persists.
有人成功在与Webpack捆绑的应用程序中为Buffer添加了polyfill吗?任何帮助将不胜感激.
Did someone succeed in adding the polyfill for Buffer in their app bundled with Webpack ?Any help would be really appreciated.
推荐答案
我刚刚通过添加
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
如此处建议 https://github.com/ipfs/js-ipfs/issues/3369#issuecomment-721975183
这篇关于如何在Wepack 5中为jsonwebtoken多缓冲缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!