I am trying to use socket.io library in a service worker of y Chrome extension, but when I try to load it in Chrome I get the error: Uncaught SyntaxError: "Cannot use import statement outside a module".
I got the library from here, then I saved the it in my extension folder, in socketIo.js:
Then in my service_worker.js, I try to import it:
import io from './socketIo.js';
const socket = io("http://localhost:3002");
Since it as service worker, I do not attach it to my extension html file, therefore I can't simply set the worker as a module like this:
<script type="module" src="./service_worker.js"></script>.
Also, since extensions are not Node.js applications, they don't have a package.json where I could set the property "type": "module.
And as you can see I used both the relative path and the extension of the file imported, as suggested here: Chrome Extension Service Worker not Supporting Importing Other JS Files or NPM Packages
Now I've added the ES module version of socket.io and its "map" file. And updated my service worker in manifest.json to be a ES module too:
But when I tried to load the extension again, I get this warning: "Service worker registration failed. Status code: 3", and the generic error: