我正在升级到 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 吗?
任何帮助将非常感激。