electron / electron

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS
https://electronjs.org
MIT License
113.83k stars 15.33k forks source link

[Bug]: Unable to register ESM module loader customization hooks in Renderer #43067

Open inukshuk opened 2 months ago

inukshuk commented 2 months ago

Preflight Checklist

Electron Version

31.2.1

What operating system(s) are you using?

Other Linux

Operating System Version

Arch Linux, macOS

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

I would like to register ESM loader customization hooks. This works fine in the main process, but fails in the renderer process because customization hooks rely on Node's worker threads internally.

Actual Behavior

Trying to register hooks in a renderer using Node's native ESM loader results in the error The V8 platform used by this instance of Node does not support creating Workers. I'm assuming this is because worker threads don't work in the renderer in general, as discussed in #18540 and I fear that the incompatibilities mention in that thread still hold true?

This would be unfortunate, since ESM loader hooks are a new feature in Node.js and I would assume that it will become more popular soon (e.g., to dynamically transform code using Babel or other compilers). If there's no feasible way to use worker threads in the renderer we should probably add a caveat to the ESM documentation that the Node loader supports customization hooks only in the main process.

Testcase Gist URL

No response

Additional Information

No response

itsdouges commented 1 week ago

This works fine in the main process

How are you using loaders in the main process? Haven't seen that specified anywhere in the Electron documentation / CLI help 👀 — would love to use the ts-node/esm one!

Trying to run electron with NODE_OPTIONS also isn't working:

NODE_OPTIONS='--no-warnings --loader ts-node/esm' electron

Results in:

Error: Cannot find module '/Users/douges/projects/triplex-monorepo/node_modules/electron/main' imported from /Users/douges/projects/triplex-monorepo/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js

😅 — will try register instead.

inukshuk commented 1 week ago

I used module.register in the main process.