0

I have an index.html file that has a script tag with type="module".

Inside this script tag, I create a simple module worker like so

    // index.html
    let processFrameWorker = new Worker("/parent/worker.js", { type: "module" });
    processFrameWorker.addEventListener('message', (event) => {
        console.table("Event", event)
        console.log("back from worker")
    })
    processFrameWorker.postMessage('Some message');

and inside my worker I have

   // worker.js
   import sayHello from "/greeting.js"
   onmessage = function (e) {
       console.log('Worker: Message received from main script');
       postMessage(sayHello());
   }

I keep coming across this error enter image description here

which points to the first line (importation line) of the worker. I am completely out of things to try on how to fix this issue. I would appreciate any help.

Noor
  • 51
  • 1
  • 6
  • 1
    With what browser have you tried this? Module workers are still not supported in Firefox... You can test support using the code in this answer of mine: https://stackoverflow.com/a/62963963/3702797 – Kaiido Jan 19 '22 at 03:27
  • 1
    Then there must be something more in your script that you are not showing to us. Does this codepen show the same error? https://codepen.io/_-0-_/pen/rNGbbOW – Kaiido Jan 19 '22 at 03:43
  • @Kaiido Thanks this was very helpful! I had known that Firefox is not supported. but my logging statements inside the worker were not appearing in chrome so I had assumed something is broken. I still do not see my logs that I do inside my worker, but I can see the value that my worker is sending back to my main thread. – Noor Jan 19 '22 at 03:43
  • @Kaiido What you mentioned worked perfectly fine for me, but now I am trying to import a node module into my worker. I could not find a way to get it done in a straightforward manner. Many people were suggesting using things like webpack or external libraries. Why can't I simply import the node module like a regular js file import? Do you have an idea? – Noor Jan 20 '22 at 23:00

0 Answers0