Service worker registration failed. Chrome extension

I don't understand how to migrate from manifest v2 to v3 in part of Service Worker. Occurs error Service worker registration failed

// manifest.json
"background": {
    "service_worker": "/script/background/background.js"
},
// background.js
chrome.runtime.onInstalled.addListener(onInstalled);

enter image description here


Solution 1:

Please find below the cause for your specific issue and the cause for not getting the details of failure in the console log.

  • Before Chrome 93, the service worker file must be in the root path where manifest.json is.

    This is a limitation of Service Worker specification, relaxed for extensions since Chrome 93.

    If, for whatever reason, you want to allow your extension to be used in older Chrome, the correct manifest.json should look like this:

    "background": {
      "service_worker": "background.js"
    },
    

    Conversely, to use an arbitrary path you need to prevent installation in older Chrome:

    "minimum_chrome_version": 93,
    "background": {
      "service_worker": "js/bg/worker-loader.js",
      "type": "module"
    },
    

    Type module is optional and is supported since Chrome 92. You can import ES modules statically for now. The support for dynamic imports is in development.

    In any Chrome version you can use importScripts('/path/foo.js', '/path/bar.js'); to import scripts from other directories.

  • If the worker script throws an error at installation, the worker won't be registered and you will not be getting the error information triggered by your service worker in the console, but only get "Service worker registration failed". This behavior is due to a bug bug in Chrome versions earlier than Chrome 93.

    Solution:

    • use Chrome 93 or newer.

    Limited workaround:

    • wrap everything in try/catch as shown in the other answer or use a "loader" script.

    Typical causes:

    • accessing an undeclared variable
    • syntax error like an unclosed parenthesis
    • accessing a chrome API without declaring it in manifest.json's permissions field
    • a crash in the worker process

Solution 2:

If the same error is still here after the script was moved to the root folder as wOxxOm mentioned, than you probably have an error in your background.js file. However at the time of this post there was no adequate error message on this, other than generic Service worker registration failed. Try to follow Simeon's workaround described here For example you could wrap your v2 background.js script into an error-catching service worker and import your old scripts in it:

// manifest.json
{
  "name": "Throw on Register!",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "background-wrapper.js"
  }
}

// background-wrapper.js file
try {
  importScripts("background.js");
} catch (e) {
  console.error(e);
}

// background.js
console.log("start");
throw new Error("lol");
console.log("end");

After that your service worker will be registered as it is as simple as possible and you will have the error info in the console. In my case it looks like that: example Happy moving to v3 :)

Solution 3:

Service workers must be registered at root level: they cannot be in a nested directory.

Since background.js is service worker now it can't be inside nested folder

Reference: https://developer.chrome.com/docs/extensions/mv3/mv3-migration-checklist/#api_checklist