olanod
olanod

Reputation: 30576

Use module scripts in web worker

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)

Upvotes: 2

Views: 2890

Answers (1)

Tomáš Zato
Tomáš Zato

Reputation: 53119

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.

Upvotes: 8

Related Questions