Cannot use import statement outside a module when importing modules in a web worker

Viewed 678

I'm trying to import a module in a web worker that imports another module and getting the following error Uncaught SyntaxError: Cannot use import statement outside a module. My web worker is importing scripts like this:

importScripts(
  './utils.js',
  './nn.js'
);

The error points to the second line with './utils.js', but its actual origin is the first line of utils.js which is the following:

import * as DG from 'datagrok-api/dg';

I tried setting "type": "module" in package.json, but it results in me being unable to compile my project with Webpack and produces other errors.

I also tried to create a worker with new Worker('my.worker.js', {type: "module"}) so I can use import statements in it, but in this case I'm unable to post a message to the worker and not even getting any error. Also module workers are not supported anywhere except for Chrome.

I tried to manage web workers with workerize-loader, but it doesn't work with Webpack 5 which I'm using.

So my question is: how can I import a module in a web worker that contains module imports?

0 Answers
Related