webpackipfsjs-ipfs

I cannot bundle ipfs-core with webpack. Module not found: Error: Can’t resolve ‘util’


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 }}
};

Solution

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