yao-pkg / pkg

Package your Node.js project into an executable
https://www.npmjs.com/package/@yao-pkg/pkg
MIT License
312 stars 11 forks source link

TypeError: Invalid host defined options when using dynamic imports #23

Closed manast closed 7 months ago

manast commented 7 months ago

What version of pkg are you using?

5.11.1

What version of Node.js are you using?

Node 20

What operating system are you using?

macOs

What CPU architecture are you using?

x86_64

What Node versions, OSs and CPU architectures are you building for?

Node 20, linux

Describe the Bug

If the code to be packaged includes a dynamic import, like this one:

const foobar = await import("foobar.js");

When running the resulting exe we get an exception with the following error:

TypeError: Invalid host defined options

Seems like for some reason the resulting exe is not compatible with dynamic imports, despite using node20 as target which has full support for dynamic imports.

Expected Behavior

It should work as it does when just running the given code with node.

To Reproduce

const foobar = await import("foobar.js");
manast commented 7 months ago

Ok, it seems like the issue comes from this Node bug: https://github.com/nodejs/node/issues/43663

robertsLando commented 7 months ago

👍🏼 keep me updated

manast commented 7 months ago

In practice, the only way to get "pkg" to work in modern codebases is to prebundle everything with esbuild and then compile the resulting bundle. It can get complicated if you have a lot of dynamic imports or you load static assets, etc, but it is doable with some non trivial effort.

robertsLando commented 7 months ago

That's exactly what i do on my packages and I have some that are really big. Take a look at this tool: https://github.com/chjj/bpkg

It's like esbuild but it handles better node native addons by loading them as base64