Quite a number of Chrome error messages have been reported by users. One of these error messages is the “unchecked runtime.lasterror: the message port closed before a response was received” error, which comes when we have issues with certain extensions.
Although Google Chrome is one of the most popular, extensively used, and inventive Internet browsers available today, it is not immune to flaws or errors in its system. While this browser is regarded as a sophisticated tool, it is susceptible to being corrupted or damaged.
This runtime.lasterror issue is somehow only visible when we right-click on the respective page, click Inspect, and then select the Console tab at the top.
Google teams update the Chrome browser on a regular basis. Because the browser may return error messages, some require extension developers to make changes to their code. This is especially true in the case of this error message: “The message port closed before a response was received“.
Sources of the runtime.lasterror problem
There are two major sources of this particular error. Let’s take a look at them and how we can tackle the error in case it’s coming from either of the sources.
Source 1: While Developing Your Personal Extensions
This error message is peculiar to the Chrome browser, and more specifically, the development of a Chrome extension. When you make use of the “browser.runtime.onMessage” promise, it appears.
You can use this event to listen to messages sent by another element of an extension you’re using. This error message will appear in the console if the function you’re defining doesn’t deliver a promise with a message.
If you’re working on a browser extension, you’ll need to change your code so that your function always returns a promise with a message, even if it’s not useful. The error message in the browser console will be removed as a result of this.
Another option is to use the “async” keyword to return an implicit promise with a message that has the value “Undefined.”
It should be noted that, even if the listener’s async code returns true, indicating that it is running an async code, the code will still contain the error “Unchecked runtime.lastError: The message port closed before a response was received”if the async code is inserted incorrectly.
Due to a misplaced async, the following code generates the error described:
Returning true will not be enough; we need to include the async code in the code body itself, not in the callback’s declaration, to solve the problem completely. Remove the async declaration from the listener and put it in the body of the code.
Source 2: While Using Third-party Extensions
What if you’re not currently developing a chrome extension? Or what if you’re not even a programmer at all? Then this message is generated by one of the browser extensions you’ve installed on your computer.
To fix this, open Google Chrome and type “chrome:/extensions” into the address bar. You can replace each extension individually once you have access to the Chrome extension and see what really matters.
Disable each extension one at a time to figure out which one is causing the problem. After disabling the extension, go back to the page where you faced the error and make a mouse movement or a click activity.
This is because errors can be caused by mouse actions. Shut down the extension that was causing the issue after you’ve figured out what was causing it.
Another approach to this could be to use chrome in incognito mode since incognito mode doesn’t come with any extension (unless you grant it access from the settings). So, if you notice that your own extension is creating these issues, double-check all of your onMessage listeners.
Some of them will most likely need to commence returning promises (indicating them as async should do just that). Also, if you aren’t building any extension, then follow the modular approach and disable every extension, then re-enable them one after the other until you find the problematic one.
Overall, when the methods above still haven’t fixed the issue, then the only solution left is to re-install chrome.