Upgrading to react-scripts v5 is not always the solution.
The full reason for this bug is described here. In short here is a brief summary:
The error is as a result of react-error-overlay (which many people would never have heard of because it is a dependency of react-scripts). This package’s dependencies were update to support webpack v5, which unfortunately is not compatible with react-scripts v4.
Method 1 (Override a package version)
If upgrading to react-scripts v5 is not working for you, you can also try another workaround which is to pin react-error-overlay to version 6.0.9:
Delete your yarn.lock or package-lock.json, then install your dependencies again.
Using yarn
yarn will take the resolutions field into consideration out of the box.
"resolutions": {
"//": "See https://github.com/facebook/create-react-app/issues/11773",
"react-error-overlay": "6.0.9"
}
For yarn workspaces, place the above resolution in the root package.json, not in the problematic folder. See this issue comment.
Using npm (>=v8.3.0)
The equivalent of resolutions for npm is overrides.
"overrides": {
"react-error-overlay": "6.0.9"
},
Using npm (<8.3.0)
You need to make sure npm uses the resolutions field when you run npm install. To automate the installation, see this answer
Method 2 (Use a webpack plugin)
Yet another (not so popular) workaround is to use a webpack plugin:
plugins:[
new webpack.DefinePlugin({
process: {env: {}}
})
]
If you use craco (v6.3.0+), you just need to modify your craco.config.js file to add that plugin:
{
...
webpack: {
plugins: {
add: [
new webpack.DefinePlugin({
process: {env: {}}
})
]
}
}
}
For customize-cra users, see this answer or this github comment.
This last method is not popular because not many CRA users ever have to touch webpack directly to work with react.