question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpack v5 support

See original GitHub issue

Polyfils aren’t no longer injected for the latest version of webpack. GatsbyJS is already using webpack v5. I’d also like to upgrade my own umd package which uses WC.

Example error message during webpack build:

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

Issue 1: Gotta have those polyfils

Rather than force every app to manually add these packages, they should already be included in the WC packages.

Issue 2: Using Node.JS modules in a browser application

Is this a concern? I read that its not recommended but haven’t researched enough to know for sure. Similar issue in another package: https://github.com/angular/angular-cli/issues/20819#issuecomment-842307462

Temporary Solution

In your application that uses Webpack V5, make the following changes

yarn add stream-browserify stream-http crypto-browserify https-browserify os-browserify util url assert

Then update your webpack config. Here’s a GatsbyJS example:

// gatsby-node.js

exports.onCreateWebpackConfig = ({ stage, actions }) => {
  actions.setWebpackConfig({
    resolve: {
      fallback: {
        util: require.resolve(`util/`),
        url: require.resolve(`url/`),
        assert: require.resolve(`assert/`),
        crypto: require.resolve(`crypto-browserify`),
        os: require.resolve(`os-browserify/browser`),
        https: require.resolve(`https-browserify`),
        http: require.resolve(`stream-http`),
        stream: require.resolve(`stream-browserify`),
      },
    },
  })
}

See stack overflow for more info

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17

github_iconTop GitHub Comments

7reactions
4skinSkywalkercommented, Oct 12, 2021

I did it differently. I’ve modified my package.json the following way, and it compiled:

{
  "name": "my-dapp",
  "version": "0.0.0",
  "browser": {
    "os": false,
    "https": false,
    "http": false,
    "stream": false,
    "util": false,
    "url": false,
    "assert": false,
    "crypto": false
  },
  ...
}

I don’t think we should depend from Node.js stuff in our browser dapp.

P.S.: after recompiling it broke again with Uncaught TypeError: util.inherits is not a function. I’ve fixed it with:

{
  "name": "ethbox-dapp",
  "version": "0.0.0",
  "browser": {
    "os": false,
    "https": false,
    "http": false,
    "stream": false,
    "util": "./node_modules/util",
    "url": false,
    "assert": false,
    "crypto": false
  },
  ...
}

P.P.S.: after compiling again it broke once again with something related to url.parse. I’ve fixed with:

"browser": {
    "os": false,
    "https": false,
    "http": false,
    "stream": false,
    "util": "./node_modules/util",
    "url": "./node_modules/url",
    "assert": false,
    "crypto": false
  },
  ...
}

It’s ridicolous that we have to deal with all this.

4reactions
rubocommented, Jun 15, 2022

For CRA v5, this can be fixed with CRACO or react-app-rewired. In case of CRACO, the .cracorc.js should look like

const { ProvidePlugin } = require('webpack');

module.exports = {
  webpack: {
    configure: {
      ignoreWarnings: [/Failed to parse source map/],
      resolve: {
        fallback: {
          assert: require.resolve('assert/'),
          crypto: require.resolve('crypto-browserify/'),
          http: require.resolve('stream-http/'),
          https: require.resolve('https-browserify/'),
          os: require.resolve('os-browserify/browser'),
          stream: require.resolve('stream-browserify/'),
          url: require.resolve('url/'),
          util: require.resolve('util/')
        }
      }
    },
    plugins: [
      new ProvidePlugin({
        Buffer: ['buffer', 'Buffer']
      })
    ]
  }
};

And the following packages must be included in the packages.json:

  • assert
  • crypto-browserify
  • https-browserify
  • os-browserify
  • stream-browserify
  • stream-http
  • url
  • util
Read more comments on GitHub >

github_iconTop Results From Across the Web

To v5 from v4 - webpack
To v5 from v4. This guide aims to help you migrating to webpack 5 when using webpack directly. ... Now let's upgrade webpack...
Read more >
Top 5 Changes in webpack v5 - Ryan H. Lewis
Webpack v5 requires a minimum of Node.js v10 to run. Node.js v10 just left LTS support in June 2021, so you should really...
Read more >
Webpack 5 Issues | Documentation - Web3Auth
js packages have certain dependencies, which are not present within the browser environment by webpack 5. Hence, you require certain node polyfills to...
Read more >
Webpack 5 Adoption - Next.js
Improved Long Term Caching of Assets: Deterministic code output that is less likely to change between builds; Improved Tree Shaking; Support for assets...
Read more >
How to polyfill node core modules in webpack 5 - Alchemy
This config-overrides.js code snippet is telling webpack how to resolve the missing dependencies that are needed to support web3 libraries and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found