'What caused the "Unchecked runtime.lastError: The message port closed before a response was received." warning?

I have searched around but it's all about people complaining the bug. Many posts say that you should check all your extensions.

However, this is something I encountered when I am developing an extension.

Here is how it happens:

I have a listener on background.js:

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
  console.log('get:', request);
  if (request.hasOwnProperty('opt')) {
    trackPage('opt/' + request.opt);
  }
  return Promise.resolve("");
});

And here is the trigger in my option page:

track('something');
function track(msg){
  chrome.runtime.sendMessage({opt: msg}, function(response) {
    console.log(response);
  });
}

The error occurs when the track function is fired.

How can I fix the error totally?



Solution 1:[1]

You can't return a Promise to make the function async, you have to return true. So change this:

return Promise.resolve("");

To this:

Promise.resolve("").then(result => sendResponse(result));
return true;

Solution 2:[2]

If you see this error on other websites too then don't be bothered because it's not generated by your app, probably some Chrome Extension.

Solution 3:[3]

I disabled Tampermonkey Chrome extension and the error disappeared.

Solution 4:[4]

check extentions chrome, update or remove or deactive

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GirkovArpa
Solution 2 Muhammad bin Yusrat
Solution 3 Ronald
Solution 4 Hossein