create-react-app: "Uncaught runtime errors" messages after upgrading to CRA 5.0.1 and Node 18
Our DevOps team informed us that they will be upgrading all servers to Node 18. Our app was working properly only with Node 16, so I upgraded CRA to 5.0.1 and Node to 18. At first everything seems fine, but then “Uncaught runtime errors” overlay messages begin popping up in development mode.
Apparently, they come up when an API service response gets cancelled. We are using Axios and axios-hooks for our service request. Clicking the close icon closes the error overlay and the app works without any issues, but it is pretty annoying for development experience. Message are always like this:
Uncaught runtime errors: × ERROR Cancel at handleError (http://localhost:3000/static/js/bundle.js:614149:58) at http://localhost:3000/static/js/bundle.js:614172:7
When I look at console, it is like this:
Uncaught (in promise) Cancel {message: undefined} message: undefined [[Prototype]]: Object
When I look at the specified rows at bundle.js, they are like this:
bundle.js:614149:58
var handleError = function handleError(error, fallbackMessage) { **var errorObject = error instanceof Error ? error : new Error(error || fallbackMessage);** var shouldDisplay = typeof options.catchRuntimeError === "function" ? options.catchRuntimeError(errorObject) : true; if (shouldDisplay) { overlayService.send({ type: "RUNTIME_ERROR", messages: [{ message: errorObject.message, stack: (0,_overlay_runtime_error_js__WEBPACK_IMPORTED_MODULE_1__.parseErrorToStacks)(errorObject) }] });
bundle.js:614172:7
(0,_overlay_runtime_error_js__WEBPACK_IMPORTED_MODULE_1__.listenToUnhandledRejection)(function (promiseRejectionEvent) { **var reason = promiseRejectionEvent.reason;** handleError(reason, "Unknown promise rejection reason"); });
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 9
- Comments: 17
Thanks a lot, I think you found a great lead for the source of the problem. At this topic I have noticed that, even though some people had been using the latest CRA for some time, they began experiencing this issue recently, after doing a fresh npm install.
I looked at webpack-dev-server release notes, and the latest v4.15.0 was released 3 weeks ago. It has only one new feature: “Overlay displays unhandled promise rejection”
Voila! This latest update seems to be the source of our problems. So, I ran this command and downgraded webpack-dev-server to the previous version:
npm install webpack-dev-server@4.14.0 --save --save-exact --save-dev
And now everything seems to be returned back to normal, and I don’t need to modify axios-interceptor. Thanks a lot for your investigation, it was great help. 🙏
EDIT: Another option is probably setting runtimeErrors to false at Webpack config. But since this is CRA, that requires a library like Craco without ejecting. I installed Craco and added below code to craco.config.js and it was successful.
module.exports = { webpack: { configure: (webpackConfig, { env, paths }) => { webpackConfig.entry = './src/index.tsx'; return webpackConfig; }, }, devServer: { client: { overlay: { errors: true, warnings: false, runtimeErrors: false, }, }, }, };
Hello, I’d like to share my findings because the same problem occurred in my project and I’ve investigated its cause.
The reason why full-screen error displays began occurring from CRA v5 is because
webpack-dev-server
, included in CRA’s dependencies, was updated from v3 to v4.overlay
? https://webpack.js.org/configuration/dev-server/#overlayAs written here, the default setting for how to display when an error or warning occurs has changed. If you change the overlay setting, you can disable the overlay display, but I think that’s a not good method.
In any case, we should fix our code that caused the error. It’s about catching exceptions that have been rejected.
I had the same issue when I was using
await
within the function that was calling an api and it did not have theasync
keyword that I forgot to add. Adding theasync
to the function fixed the issue. I hope this can help someone!Having exactly the same issue after upgrading react-scripts to 5.0.0
@leticiafontoura I did the same but didn’t work, but then I found this version of the same code in stack overflow and worked for me. I hope you find it useful too.
@JhordanOjeda @keremcanb I actually would like to avoid the overlay message hahaha on Friday it wasn’t showing if a request got canceled but yesterday, there it was. all I did was delete my node_modules and package.lock because of reasons and then this started
here’s my code:
I replaced the console.log in my code with your throw code like this:
axios.interceptors.response.use((error: AxiosError) => { if (Axios.isCancel(error)) { throw new axios.Cancel('Operation canceled by the user.'); } });
This means again it will show overlay cancel messages, which was the main problem of my original question.
But I also had a more serious problem. I was getting Uncaught TypeError: Cannot read properties of undefined (reading ‘data’) messages and it was causing white screen at the actual app, not only in development mode.
So now I’m back to point one, again having overlay cancel messages, but at least our app is not throwing white screen anymore. So thanks for this solution.
My colleague added an Axios.isCancel handler to the interceptor and it seems the problem is solved. Cancel error messages are not showing at overlay anymore.
Same here as well. The app shows the error overlay when an endpoint call happens. Also clicking the ‘x’ button at the top right closes the error overlay and the app works without any issues.