Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V4] Message formatting is incompatible with Webpack v5 #9880

Closed
femesq opened this issue Oct 23, 2020 · 15 comments · Fixed by #10121
Closed

[V4] Message formatting is incompatible with Webpack v5 #9880

femesq opened this issue Oct 23, 2020 · 15 comments · Fixed by #10121

Comments

@femesq
Copy link

femesq commented Oct 23, 2020

TypeError: message.split is not a function

error occurs when webpack throws any error during compilation

Seems like this line should check if message is an object with and message key, as webpack v5 mentions in migration guide:

Stats json errors and warnings no longer contain strings but objects with information splitted into properties.
MIGRATION: Access the information on the properties. i. e. message

Environment

webpack: "5.2.0"
react-dev-utils: "11.0.0"

This was referenced Oct 27, 2020
femesq added a commit to femesq/create-react-app that referenced this issue Oct 28, 2020
Fix formatting error on webpack v5, as described here: facebook#9880

Specifically in [this link](https://webpack.js.org/blog/2020-10-10-webpack-5-release/#minor-changes), on the item that says:

Stats json errors and warnings no longer contain strings but objects with information splitted into properties.
MIGRATION: Access the information on the properties. i. e. message
@jardicc
Copy link

jardicc commented Dec 10, 2020

When I change:

let lines = message.split('\n');

into:

message = message.message;
let lines = message.split('\n');

In: node_modules\react-dev-utils\formatWebpackMessages.js
It works again for me.

Hopefully, it will be fixed soon :-)

@Lyfme
Copy link

Lyfme commented Dec 15, 2020

I have the same question.And I can't modify the formatWebpackMessages.js in everyone'node_modules of the team.Please upgrade soon.

@nlozovan
Copy link

@Lyfme you shouldn't change the module code. Instead, change the input format:

const rawMessages = stats.toJson({moduleTrace: false}, true);

From:

const messages = formatWebpackMessages(rawMessages);

To:

const messages = formatWebpackMessages({
   errors: rawMessages.errors.map((e) => e.message),
   warnings: rawMessages.warnings.map((e) => e.message),
 });

The formatWebpackMessages plugin, according to this link, expects an object with 2 props, thus you can push these directly with the expected format.

Hope that helps.

@pubdreamcc
Copy link

I have the same question, please upgrade soon!

@femesq
Copy link
Author

femesq commented Jan 8, 2021

For those waiting for the fix to be accepted, you can use a postinstall script like this:

  • tools/postinstall.js:
const replace = require('replace-in-file');

const fixFormatWebpackMessages = async () => {
  try {
    const results = await replace({
      files: 'node_modules/react-dev-utils/formatWebpackMessages.js',
      from: `let lines = message.split('\\n');`,
      to: `let lines = [];

  if (typeof message === 'string' || message instanceof String) {
    lines = message.split('\\n');
  } else if ('message' in Object.keys(message)) {
    lines = message['message'].split('\\n');
  }`,
    });

  } catch (e) {
    console.log('error while trying to fix  "formatWebpackMessages.js"', e);
  }
};

fixFormatWebpackMessages();

and on you package.json, add this to the scripts section:

"scripts": {
    ....
    "postinstall": "node tools/postinstall.js",
    ....
  },

vagrantsn added a commit to pagarme/react-scripts-former-kit-dashboard that referenced this issue Feb 10, 2021
This is meant to be a temporary fix until this issue is not solved: facebook/create-react-app#9880
vagrantsn added a commit to pagarme/react-scripts-former-kit-dashboard that referenced this issue Feb 10, 2021
This is meant to be a temporary fix until this issue is not solved: facebook/create-react-app#9880
vagrantsn added a commit to pagarme/react-scripts-former-kit-dashboard that referenced this issue Feb 11, 2021
This is meant to be a temporary fix until this issue is not solved: facebook/create-react-app#9880
@filipmacek
Copy link

When we can hope to see getting this resolved. It really easy fix.
I am getting tired of inserting message = message.message; everytime I got error messsage.split is not a function becase formating webpack messages is not compatible with webpack 5
Thanks

@KurtGokhan
Copy link

For now, the cleanest workaround hack dirty solution is:

  • Get the tgz of react-dev-utils with the fix
  • Upload it to a temporary path in your repo like /temp/react-dev-utils-hotfix.tgz
  • Add this line to your package.json:
  "resolutions": {
    "react-dev-utils": "https://github.com/mycompany/myrepo/raw/master/temp/react-dev-utils-hotfix.tgz"
  }

Hoping that #9943 will be merged soon and we will not need this anymore.

@utterly-calm
Copy link

When is it getting fixed. Should I downgrade webpack or react-dev-utils to make it work temporarily and to what version?

@utterly-calm
Copy link

It is causing a real trouble. Please fix it.

@alex521
Copy link

alex521 commented Apr 9, 2021

When is it getting fixed?

@Obi-Dann
Copy link

Obi-Dann commented May 20, 2021

Got another workaround without modifying files in node_modules. It changes webpack.stats.toJson() errors and warning arrays from objects to strings. It's a webpack plugin that should be added to the array of plugins in your webpack config

// @ts-check

/**
 * formatWebpackMessages helper from Create-react-app expects errors and warnings to be
 * arrays of strings as they are in Webpack 4.
 * Webpack 5 changed them to objects.
 * This plugin changes them back to strings until the issue is resolved
 * https://github.com/facebook/create-react-app/issues/9880
 */
class FixMessageFormatterPlugin {
    /** @type {import('webpack').WebpackPluginFunction} */
    apply = compiler => {
        compiler.hooks.compilation.tap('FixMessageFormatterPlugin', compilation => {
            compilation.hooks.statsFactory.tap('FixMessageFormatterPlugin', stats => {
                stats.hooks.result
                    .for('error')
                    .tap('FixMessageFormatterPlugin', (obj, data, ctx) =>
                        formatError(obj, 'ERROR'),
                    );

                stats.hooks.result
                    .for('warning')
                    .tap('FixMessageFormatterPlugin', (obj, data, ctx) =>
                        formatError(obj, 'WARNING'),
                    );
            });
        });
    };
}

function formatError(obj, prefix = '') {
    const moduleName = obj.moduleName;
    const location = obj.loc;

    if (moduleName) {
        prefix = (prefix + ` in ${moduleName}`).trim();

        if (location) {
            prefix = (prefix + `:${location}`).trim();
        }
    }

    return `${prefix ? prefix + '\n' : ''}${obj.message}`;
}

module.exports = FixMessageFormatterPlugin;

@darrylsepeda
Copy link

This issue already closed, but I still encounter it even with the latest version of react-dev-utils..

@filipmacek
Copy link

Yes, this is still a problem on version 11.0.4

@Enfield1
Copy link

@filipmacek, @darrylsepeda
You can use version 12.0.0-next.47. This bug is fixed there

@pringshia
Copy link

pringshia commented Nov 22, 2021

Would it also be possible to fix this for version 11.x so as not to introduce the other breaking changes that are a part of version 12.x? My use-case is I'm in a yarn 2 monorepo setup sharing react-dev-utils across workspaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet