xan105 / node-nodeRT

NodeRT but precompiled (use WinRT API directly from node)
Other
3 stars 2 forks source link

Using `nodert` with CJS #4

Closed ShafSpecs closed 6 months ago

ShafSpecs commented 6 months ago

I know this package is ESM-only (as node ecosystem should be), but I am wondering how I could make it work in a CJS environment. Won't mind forking and doing the edits just for my project. I am using the package in an electron app - and due to the nature of electron and the electron flavour I am using (vite), I can't transform the main process into esm, so I am stuck with cjs.

Initially tried to do dynamic import: await import('...') which works in development, but the moment I try it in production, it fails:

UnhandledPromiseRejectionWarning: TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    at importModuleDynamicallyCallback (node:internal/modules/esm/utils:213:9)
    at subscribeToGeolocation (evalmachine.<anonymous>:1:4739)
    at evalmachine.<anonymous>:1:456440

The code is obfuscated in production btw (bytecode) and then loaded by node vm: https://www.mo4tech.com/electron-code-protection-solution-based-on-node-js-addon-and-v8-bytecode.html But it seems that process breaks dynamic imports, so now I am stuck with trying to make it work with CJS instead. Thank you for this package btw 😄, much appreciated

xan105 commented 6 months ago

Yes, dynamic import: await import('...') is the only way I know of. Have to make it a dual CJS/ESM package for your use case (?).

Would changing all of the exports from

{
  ".": {
     "types": "./types/index.d.ts",
     "default": "./lib/index.js"
  },
  ...
}

to

{
  ".": {
     "require": "./lib/index.cjs",
     "import": "./lib/index.js",
     "types": "./types/index.d.ts"
  },
  ...
}

be enough for vite ? Bundler wise I only know some esbuild. If so I'll try to remember how to CJS and add it to the generate exports script.

(I went all-in when esm was available, I haven't used CJS in years).

xan105 commented 6 months ago

🤔Is this https://nodejs.org/api/vm.html#when-importmoduledynamically-is-a-function related to your problem ?

When importModuleDynamically is a function, it will be invoked when import() is called in the compiled code for users to customize how the requested module should be compiled and evaluated. Currently, the Node.js instance must be launched with the --experimental-vm-modules flag for this option to work. If the flag isn't set, this callback will be ignored. If the code evaluated actually calls to import(), the result will reject with ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG.

Node arg can be passed to electron using --js-flags="foo=bar" ex: electron --js-flags="--env-file=.env" .

ShafSpecs commented 6 months ago

🤔Is this https://nodejs.org/api/vm.html#when-importmoduledynamically-is-a-function related to your problem ?

When importModuleDynamically is a function, it will be invoked when import() is called in the compiled code for users to customize how the requested module should be compiled and evaluated. Currently, the Node.js instance must be launched with the --experimental-vm-modules flag for this option to work. If the flag isn't set, this callback will be ignored. If the code evaluated actually calls to import(), the result will reject with ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG.

Node arg can be passed to electron using --js-flags="foo=bar" ex: electron --js-flags="--env-file=.env" .

Seems like it is the issue. 🤔, I am wondering how I can now append this flag to my production executable so that it always get run. The package doesn't seem like it would be receiving updates anymore so it would always be experimental.

Or I just remove the bytecode protection and see how that goes

Edit: Passing the flag didn't seem to fix the issue. Passed it multiple ways, via the bytecode loader, script itself, electron switches and more. But it didn't work. Such a shame that node leaves their work in shambles atimes 😅

ShafSpecs commented 6 months ago

Looking at: https://nodejs.org/docs/latest-v21.x/api/vm.html#vmconstantsuse_main_context_default_loader, this should solve the issue. Except node 21.7.3 throws undefined error, this is a real headscratcher and atp out of the scope of this project. Thanks Anthony 👋