[SOLVED] Enable node integration in Web Worker?


#1

I read the docs, and I have enabled node integration:

const win = new Electron.BrowserWindow({
	show: false,
	webPreferences: {
		// allow code inside this window to use use native window.open()
		nativeWindowOpen: true,
		nodeIntegrationInWorker: true,
	},
})

In the window, I call new Worker('file:///path/to/file.js')

However, it doesn’t seem to work; I get errors in the worker like

Uncaught ReferenceError: require is not defined

on lines like

const _ = require("lodash")

Is require supposed to work in a Worker? How do we make it work?


#2

Nevermind, require inside a Worker works!

I made a bare minimal example here: https://github.com/trusktr/electron-web-worker-example.

There must be something wrong in my actual project (which is huge compared to that example, and is doing some custom TypeScript transpilation).