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.