Webpack 5 errors

If you're using the latest version of create-react-app with the @imtbl/XpansionChain-sdk module, you may see errors like this when trying to start up the app:

Module not found: Error: Can't resolve 'https' in '/Users/{username}/{project-name}/node_modules/@imtbl/imx-sdk/dist'
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.

Why is this happening?arrow-up-right

The reason for this error is that create-react-app uses a version of webpack greater than 5, which, unlike versions < 5, does not include Node.js polyfills by default. This means that they need to be configured manually for each module that requires them.

Normally, this involves updating the webpack config file inside a project, however, create-react-app uses another package called react-scripts to manage webpack (and other build dependencies). As we cannot update the webpack config within react-scripts, we will need to override it.

How to fix thisarrow-up-right

1. Install react-app-rewiredarrow-up-right

This package allows us to update the webpack config file to fix the polyfill node core module error.

Install with npm:

npm install --save-dev react-app-rewired

Install with yarn:

yarn add --dev react-app-rewired

2. Install the missing dependenciesarrow-up-right

The following missing dependencies will have to be installed: crypto-browserify, stream-browserify, assert, stream-http, https-browserify, os-browserify, url, process

Install with npm:

Install with yarn:

3. Override the create-react-app webpack config filearrow-up-right

This is how we override the webpack config file in react-scripts and tell it how to resolve the missing polyfill dependencies.

In the root folder of your project, create a new file called config-overrides.js, and add the following code to it:

4. Override package.json to include the webpack configurationarrow-up-right

Now, to implement the new config, we need to call react-app-rewired instead of react-scripts in the following scripts in our package.json:

  • start

  • build

  • test

This is what the package.json file looks like before:

This is what it looks like after:

Once you've done this, the development server should be up and running again.

How to deal with 'failed to parse source map' warningsarrow-up-right

You will still have a large amount of failed to parse source map from modules warnings. They can be ignored for now, however, should you want to get rid of them, you can disable them (see this discussion) by adding GENERATE_SOURCEMAP=false to the start script in package.json:

Last updated