1

There are a bunch of similar threads on here however I cannot seem to find anything that works for me. I am building a chrome extension where I'd like to connect to a websocket. I have found a project, https://github.com/matthewlawson/lnm-socket.io, which I try to use as inspiration although I cannot make it work for me.

The server is not in the chrome extension, however I try to access it from my background.js file. That's when I get the Cannot use import statement outside a module error message. Adding "type": "module" to my package.json file doesn't seem to fix it and since it is the background file I do not have it in a script tag to place the "type": "module" in. What can I do?

Evelina K
  • 11
  • 1
  • 2
  • 1
    Does this answer your question? [chrome extension mv3 - Modularize service worker js file](https://stackoverflow.com/questions/66406672/chrome-extension-mv3-modularize-service-worker-js-file) - add `"type": "module"` to the `background` section in manifest.json. – wOxxOm May 03 '22 at 16:41
  • Adding it to the manifest.json surely did something, however, I still cannot "import io from 'socket.io-client'; " since it doesn't start with a / I find this peculiar since it works in this 5 year old project. I guess it might have to do with the new version not supporting it yet, but I am here to ask - is there a way for me to do this that I am not aware of? – Evelina K May 04 '22 at 07:37
  • You can't use DOM-based libraries in a service worker. Find a compatible one. – wOxxOm May 04 '22 at 09:17
  • Any suggestions? Feels like I've searched the whole internet to find a server - client solution to chrome extensions that are not deprecated... – Evelina K May 04 '22 at 10:28
  • @wOxxOm how can I do with a contentscript? – Volatil3 Jul 31 '23 at 18:42
  • [How to import ES6 modules in content script for Chrome Extension](https://stackoverflow.com/a/53033388) – wOxxOm Jul 31 '23 at 20:31

0 Answers0