Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
How to import functions to web worker?

I need to use two modules, originally in Typescript, inside my Javascript web worker. Webpack seems to be taking care about the compatibility problem. Now, to use the modules, I've tried using both require and import.

require gives me Uncaught ReferenceError: require is not defined, which, according to what I found, is because you can't use it on the browser, therefore import is the viable option. import, however, gives me Uncaught SyntaxError: Cannot use import statement outside a module, to which I found a possible solution in here, but while adding { type: "module" } to the constructor does make the error go away, it also makes it so that my worker stops functioning.

In the file that calls worker = new Worker(path, { type: "module" }), I have a worker.postMessage(msg) call. In the worker, there is a

self.addEventListener('message', (e) => {
    doThingsWith(e)
    [...]
    self.postMessage(something)
}

event listener. From the moment I add type: "module", anything inside the event listener does not work.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!