我无法将 ipfs-core 与 webpack 捆绑在一起。找不到模块:错误:无法解析“util”;
我想在我的应用程序的浏览器端使用 ipfs-cose。我使用 webpack 作为捆绑器,但对于 ipfs-core 需要一个 polyfill,
Module not found: Error: Can't resolve 'util' .......
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
**If you want to include a polyfill, you need to:**
** - add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'**
** - install 'util'**
**If you don't want to include a polyfill, you can use an empty module like this:**
** resolve.fallback: { "util": false }**
我尝试了建议的两个选项。 使用 resolve.fallback: { "util": require.resolve("util/") }
在我的网页中导入 ipfs-core 会引发此错误
Uncaught ReferenceError: process is not defined
at eval (webpack://stellarnft/./node_modules/util/util.js?:109:1)
at Object../node_modules/util/util.js (create.js:8274:1)
at __webpack_require__ (create.js:14481:42)
at eval (webpack://stellarnft/./node_modules/levelup/lib/levelup.js?:4:19)
at Object../node_modules/levelup/lib/levelup.js (create.js:3948:1)
at __webpack_require__ (create.js:14481:42)
at eval (webpack://stellarnft/./node_modules/level-packager/level-packager.js?:3:17)
at Object../node_modules/level-packager/level-packager.js (create.js:3894:1)
at __webpack_require__ (create.js:14481:42)
at eval (webpack://stellarnft/./node_modules/level/browser.js?:1:18)
,并且使用 resolve.fallback: { "util": false }
我得到
Uncaught TypeError: inherits is not a function
at eval (webpack://stellarnft/./node_modules/levelup/lib/levelup.js?:81:1)
at Object../node_modules/levelup/lib/levelup.js (create.js:3806:1)
at __webpack_require__ (create.js:14285:42)
at eval (webpack://stellarnft/./node_modules/level-packager/level-packager.js?:3:17)
at Object../node_modules/level-packager/level-packager.js (create.js:3752:1)
at __webpack_require__ (create.js:14285:42)
at eval (webpack://stellarnft/./node_modules/level/browser.js?:1:18)
at Object../node_modules/level/browser.js (create.js:3773:1)
at __webpack_require__ (create.js:14285:42)
at eval (webpack://stellarnft/./node_modules/datastore-level/esm/src/index.js?:11:63)
这是我的 webpack 配置
module.exports = {
entry: {
wallet: './src/client/wallet.js',
account: './src/client/account.js',
topButtons: './src/client/topButtons.js',
create: './src/client/create.js',
artistInfo: './src/client/artistInfo.js'
},
output: {
filename: '[name].js',
path: __dirname + '/dist/js',
},
experiments: {
topLevelAwait: true,
},
resolve: { fallback: { "util": require.resolve("util/") } }
//resolve: {fallback: { "util": false }}
};
I want to use ipfs-cose in browser side of my app. I use webpack as a bundler but for ipfs-core is required a polyfill
Module not found: Error: Can't resolve 'util' .......
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
**If you want to include a polyfill, you need to:**
** - add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'**
** - install 'util'**
**If you don't want to include a polyfill, you can use an empty module like this:**
** resolve.fallback: { "util": false }**
I tried with both options proposed.
With resolve.fallback: { "util": require.resolve("util/") }
the import of ipfs-core in my web page throws this error
Uncaught ReferenceError: process is not defined
at eval (webpack://stellarnft/./node_modules/util/util.js?:109:1)
at Object../node_modules/util/util.js (create.js:8274:1)
at __webpack_require__ (create.js:14481:42)
at eval (webpack://stellarnft/./node_modules/levelup/lib/levelup.js?:4:19)
at Object../node_modules/levelup/lib/levelup.js (create.js:3948:1)
at __webpack_require__ (create.js:14481:42)
at eval (webpack://stellarnft/./node_modules/level-packager/level-packager.js?:3:17)
at Object../node_modules/level-packager/level-packager.js (create.js:3894:1)
at __webpack_require__ (create.js:14481:42)
at eval (webpack://stellarnft/./node_modules/level/browser.js?:1:18)
and with resolve.fallback: { "util": false }
I get
Uncaught TypeError: inherits is not a function
at eval (webpack://stellarnft/./node_modules/levelup/lib/levelup.js?:81:1)
at Object../node_modules/levelup/lib/levelup.js (create.js:3806:1)
at __webpack_require__ (create.js:14285:42)
at eval (webpack://stellarnft/./node_modules/level-packager/level-packager.js?:3:17)
at Object../node_modules/level-packager/level-packager.js (create.js:3752:1)
at __webpack_require__ (create.js:14285:42)
at eval (webpack://stellarnft/./node_modules/level/browser.js?:1:18)
at Object../node_modules/level/browser.js (create.js:3773:1)
at __webpack_require__ (create.js:14285:42)
at eval (webpack://stellarnft/./node_modules/datastore-level/esm/src/index.js?:11:63)
This is my webpack config
module.exports = {
entry: {
wallet: './src/client/wallet.js',
account: './src/client/account.js',
topButtons: './src/client/topButtons.js',
create: './src/client/create.js',
artistInfo: './src/client/artistInfo.js'
},
output: {
filename: '[name].js',
path: __dirname + '/dist/js',
},
experiments: {
topLevelAwait: true,
},
resolve: { fallback: { "util": require.resolve("util/") } }
//resolve: {fallback: { "util": false }}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我找到了解决方案。 webpack 5+ 需要 node-polyfill-webpack-plugin 用于 polyfill。
Ok I found the solution. webpack 5+ requires node-polyfill-webpack-plugin for polyfills.