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.

Error compiling with CreateReactApp: 'minification failed' on scatter-js. Workaround: transpile with 'babel-preset-2015'

See original GitHub issue

Using create react app and scatter-js, the build process fails on production builds when it attempts to minify/uglify the build.

Here is the documentation snippet from Facebook on the matter: https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-build-fails-to-minify

npm run build fails to minify Some third-party packages don’t compile their code to ES5 before publishing to npm. This often causes problems in the ecosystem because neither browsers (except for most modern versions) nor some tools currently support all ES6 features. We recommend to publish code on npm as ES5 at least for a few more years.

To resolve this: Open an issue on the dependency’s issue tracker and ask that the package be published pre-compiled. Note: Create React App can consume both CommonJS and ES modules. For Node.js compatibility, it is recommended that the main entry point is CommonJS. However, they can optionally provide an ES module entry point with the module field in package.json. Note that even if a library provides an ES Modules version, it should still precompile other ES6 features to ES5 if it intends to support older browsers. Fork the package and publish a corrected version yourself.

If the dependency is small enough, copy it to your src/ folder and treat it as application code.

In the future, we might start automatically compiling incompatible third-party modules, but it is not currently supported. This approach would also slow down the production builds.

For other developers / information purposes, the workaround that ended up working for me is the following:

There are 3 packages we need to transpile using babel-preset-2015:

  • scatter-js
  • web3-provider-engine
  • eth-block-tracker

Resolution Steps

  • npm install --save babel-preset-es2015
  • Add the following additional entries in /config/paths.js in the module exports:
module.exports = {
...
  scatterSrc: resolveApp('node_modules/scatter-js'),
  web3Src: resolveApp('node_modules/web3-provider-engine'),
  ethBlockTrackerSrc: resolveApp('node_modules/eth-block-tracker'),
};
  • Open /config/webpack.config.prod.js, find this section:
...
      {
        // "oneOf" will traverse all following loaders until one will
        // match the requirements. When no loader matches it will fall
        // back to the "file" loader at the end of the loader list.
        oneOf: [
...

Add a new babel loader entry for the paths we created:

          {
            test: /\.(js|jsx|mjs)$/,
            include: [
              paths.scatterSrc,
              paths.web3Src,
              paths.ethBlockTrackerSrc
            ],
            loader: require.resolve('babel-loader'),
            query: {
              presets: ['babel-preset-es2015'],
            }
          },

After this you should be able to run npm run build without any more issues.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
acouttscommented, Oct 29, 2018

@brandedux @friedger yes eject CRA first.

1reaction
ChenLi0830commented, Sep 19, 2018

Thank you for that @acoutts ! It worked for me. I wonder if that error is caused because scatter-js didn’t compile their code to ES5 before publishing to npm as indicated in the facebook instruction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Failed to minify the bundle" error on build (related to Terser)
yarn build works for me right after npx create-react-app , but fails if I delete yarn.lock and node_modules , and reinstall the modules...
Read more >
My create-react-app is failing to compile due to ESLint error
Solution : This issue has been fixed in the react-scipts:"4.0.3" but, the eslint errors present in the project are ...
Read more >
Troubleshooting
If this doesn't happen, try one of the following workarounds: Check that your file is imported by your entrypoint. TypeScript will show errors ......
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