nwjs / nw.js

Call all Node.js modules directly from DOM/WebWorker and enable a new way of writing applications with all Web technologies.
https://nwjs.io
MIT License
40.39k stars 3.88k forks source link

Node flag "--experimental-require-module" isn't actually used by NW.js #8237

Open vankasteelj opened 3 hours ago

vankasteelj commented 3 hours ago

Issue Type

Current/Missing Behavior

the node>22.x flag --experimental-require-module can't seem to be used on NW.js .

Using either in package.json: "node-main": "--experimental-require-module" Or with the CLI ./nw.exe --experimental-require-module

The error thrown when require() is used in the app is require() of ES Module. Using the same flag with node (and not NWjs!!) gives the error require() cannot be used on an ESM graph with top-level await. Use import() instead.

Which makes me believe that the flag, although present in process.execArgv and detected by node in CLI, is never actually used by NW.js.

Expected/Proposed Behavior

Passing the experimental flag --experimental-require-module (which is no longer needed in node>23.x, BTW, but that flag thing might be deeper than just that? Idk.) should activate the require() of ES Modules.

Additional Info

Tests were done using require('webtorrent') (v.2.5.7, which is an async ES module) either in node (working but throws an async error, which is expected) or in NW.js' devtools (not working at all) nw_n02OBrbrGn

vankasteelj commented 2 hours ago

Actually some other things that "should" work in Node don't work in NWjs. Take this example, that works fine in Node, but not in NW.js (to load an ESM in CJS, here the example is with webtorrent again) :

//note: copyright-free 'big buck bunny' cartoon
var magnet = 'magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c&dn=Big+Buck+Bunny&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fbig-buck-bunny.torrent';

var Webtorrent = await import('webtorrent');
var Client = new Webtorrent.default(); 

Client.add(magnet, (torrent) => {
  console.log('torrent is', torrent) // <== this never happens in NW.js, but happens in Node
})