我无法将 ipfs-core 与 webpack 捆绑在一起。找不到模块:错误:无法解析“util”;

发布于 2025-01-15 09:06:47 字数 3005 浏览 2 评论 0原文

我想在我的应用程序的浏览器端使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宛菡 2025-01-22 09:06:47

好吧,我找到了解决方案。 webpack 5+ 需要 node-polyfill-webpack-plugin 用于 polyfill。

Ok I found the solution. webpack 5+ requires node-polyfill-webpack-plugin for polyfills.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文