I am constructing a desktop application using Electron in tandem with ReactJS.
I open a new, invisible BrowserWindow from the first renderer process to launch another renderer process. In this new renderer process, I register a web worker where an API call will be periodically called to fetch data.
This is the code in the invisible window HTML script tag to register the web worker:
let installWebWorker = () => {
console.log("installWebWorker()");
if (typeof(Worker) !== "undefined") {
console.log("Web worker supported");
let monitorsWorker = new Worker("fetchMonitors.js");
} else {
console.log("Sorry! No Web Worker support...");
}
}; //end installWebWorker()
installWebWorker();
In the fetchMonitors.js worker itself, I require the "electron-is-dev" node module to make the fetch API call in development. To do so, I use require("electron").remote.require("electron"). I obtain the following error:
DevTools Error:
This error leads me to believe that Electron NodeJS modules are not supported in web workers. The Electron documentation here says otherwise, however.
I was able to solve my own issue. It turns out as long as you enable nodeIntegration in your background window to run the web worker, you can actually use NodeJS in the web worker.
What was throwing my error was that I was requiring the node modules via electron.remote(). In the web worker, you can just import node modules as you normally would in node, with require().
This repository details how to use node modules in web workers inside Electron.