Can't load worker (modulePath) with in ES module scope
Describe the bug
Hi, I'm trying to load a worker from the main process with an ESM ("type": "module") project, using the following syntax:
import workerPath from "./native-worker?modulePath";
...
When starting the app in dev mode, I get:
App threw an error during load
ReferenceError: __dirname is not defined in ES module scope
Note: using a ?nodeWorker style import seem to work, but apparently it's not the recommended way to go (source).
Thanks
Electron-Vite Version
4.0.0
Electron Version
36.5.0
Vite Version
7.0.4
Validations
- [x] Follow the Code of Conduct.
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
This might be a bug. CJS syntax compatibility isn't working in the worker. You can use the following ESM syntax instead.
import.meta.dirname
There is no __dirname code in my worker, it's generated by electron-vite.
// worker.ts
import { parentPort } from "worker_threads";
parentPort.postMessage("It works!");
// worker consumer
import workerPath from "./worker?modulePath";
...
// generated index.js
...
const workerPath = join$1(__dirname, "./worker-BsYDk9WK.js");
...
Same issue here. [email protected] & [email protected]
fixed, electron-vite 5