0

what i am trying to achieve is to import an npm module inside my React Web Worker using require('package-name').

But it gives me the following error Uncaught (in promise) ReferenceError: require is not defined How can i use a module inside a worker, is there a native way or some way to bypass it?

  • Maybe this will help https://stackoverflow.com/questions/19059580/client-on-node-js-uncaught-referenceerror-require-is-not-defined – Disco Nov 03 '22 at 14:51
  • You can either bundle the code that runs in the worker with a bundler like webpack or rollup, or you can use `importScripts` ([see this](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts)). There are also ModuleWorkers, although they don't currently work properly even in chrome and I'll believe them when I see them. – Jared Smith Nov 03 '22 at 14:54

0 Answers0