Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension: Uncaught SyntaxError: Cannot use import statement outside a module

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?

like image 207
Evelina K Avatar asked Feb 14 '26 05:02

Evelina K


1 Answers

The solution I found was not to change the manifest.json; instead, it was how the javascript file was loaded.

I went from <script src="/dist/popup.js"></script>

to <script type="module" src="/dist/popup.js"></script>

like image 191
Shadoath Avatar answered Feb 16 '26 19:02

Shadoath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!