Use module scripts in web worker

Viewed 2525

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)
1 Answers

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.
Related