2

Web worker scripts are loaded when a Worker is instantiated but how can I use a script that is a module so I don't get an error? (assume the browser supports modules).

const worker = new Worker('my-worker.js')

In my-worker.js

import {foo} from 'foo.js'
console.log(foo)
olanod
  • 30,306
  • 7
  • 46
  • 75
  • Possible duplicate of [Web Workers - How To Import Modules](https://stackoverflow.com/questions/44118600/web-workers-how-to-import-modules) – colxi Oct 24 '18 at 19:36

1 Answers1

8

Use the type option:

const worker = new Worker('my-worker.js', {type:"module"});
  • type: A DOMString specifying the type of worker to create. The value can be classic or module. If not specified, the default used is classic.
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778