1

How can i require an npm module that you would normally do with cosnt package = require('package-name') but inside a web worker?

I've been struggling with this and every time i get the following error: ReferenceError: __webpack_require__ is not defined Any help would be appreciated :)

Update: Here is the worker:

async function handleMessage(dates){
const package = require('package-name')
//.....
}
export default handleMessage;

App.js:

const [calculateStatus, setCalculateStatus] = React.useState(false);
const [calculateWorker,
{ status: calculateWorkerStatus, kill: killWorker }
] = useWorker(handleMessage, {
timeout: 5000
});

 const Test = () => {
  calculateWorker(dates).then(result => {
    console.log("Worker result", result);
  });
};

I used create-react-app so webpack config is a bit tricky. I tried using React-app-rewired and tried the following override, but no luck:

module.exports = function override(config, env) {
config.node = { global: false, mode:"production"}
return config

}

  • Can you provide a code snippet or any more detail on when this is appearing? If you're using webpack, do you have a `webpack.config.js` you can share? Have you checked similar questions such as [this](https://stackoverflow.com/questions/53890392) or [this?](https://stackoverflow.com/questions/66159523) – Chris Dueck Nov 17 '22 at 18:35
  • @ChrisDueck I edited the post with some more information and code from the worker and app.js. I am really stuck on this and i need it done :( – johnnybeast Nov 18 '22 at 13:10

0 Answers0