pkg icon indicating copy to clipboard operation
pkg copied to clipboard

Maybe npm module with dynamic import is not included in binary

Open kochizufan opened this issue 3 years ago • 4 comments

What version of pkg are you using?

5.8.0

What version of Node.js are you using?

16.18.1

What operating system are you using?

Windows

What CPU architecture are you using?

x86_64

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

node16-win-x64

Describe the Bug

In my app, I import the npm module dynamically like:

const main = async () => {
  const { geojson } = await import("flatgeobuf");
  console.log("It works!");
};

main();

But this causes error when I run the app of compiled result.

pkg/prelude/bootstrap.js:1930
      return wrapper.apply(this.exports, args);
                     ^

TypeError: Invalid host defined options
    at main (C:\snapshot\src\test.js)
    at Object.<anonymous> (C:\snapshot\src\test.js)
    at Module._compile (pkg/prelude/bootstrap.js:1930:22)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.runMain (pkg/prelude/bootstrap.js:1983:12)
    at node:internal/main/run_main_module:17:47

Once I commented out this dynamic import, app works well. No error.

So I dought compiled image not included target module.

I'm sure to add

  "pkg": {
    "assets": ["node_modules/flatgeobuf/**/*"]
  },

to my package.json, but there are no changes.

Expected Behavior

Successfully run without error.

To Reproduce

$ npm install flatgeobuf
  1. Add this to package.json
  "pkg": {
    "assets": ["node_modules/flatgeobuf/**/*"]
  },
  1. Create src/test.js with this content:
const main = async () => {
  const { geojson } = await import("flatgeobuf");
  console.log("It works!");
};

main();
  1. Compile
pkg src/test.js --target=node16-win-x64 --output=dist/test.exe
  1. Executing dist/test.exe causes error:
pkg/prelude/bootstrap.js:1930
      return wrapper.apply(this.exports, args);
                     ^

TypeError: Invalid host defined options
    at main (C:\snapshot\src\test.js)
    at Object.<anonymous> (C:\snapshot\src\test.js)
    at Module._compile (pkg/prelude/bootstrap.js:1930:22)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.runMain (pkg/prelude/bootstrap.js:1983:12)
    at node:internal/main/run_main_module:17:47
  1. Comment out dynamic import:
const main = async () => {
  // const { geojson } = await import("flatgeobuf");
  console.log("It works!");
};

main();
  1. Compile
pkg src/test.js --target=node16-win-x64 --output=dist/test.exe
  1. Executing dist/test.exe:
It works!

kochizufan avatar Dec 19 '22 15:12 kochizufan

I have the same problem with the usage of pMap, which also requires dynamic imports. @kochizufan did you find a workaround for this issue meanwhile?

albertininm avatar Feb 01 '23 10:02 albertininm

Here a hint about how to fix that: https://github.com/vercel/pkg/issues/1419#issuecomment-997982346

robertsLando avatar Feb 01 '23 10:02 robertsLando

This issue is stale because it has been open 90 days with no activity. Remove the stale label or comment or this will be closed in 5 days. To ignore this issue entirely you can add the no-stale label

github-actions[bot] avatar May 03 '23 00:05 github-actions[bot]

This issue is now closed due to inactivity, you can of course reopen or reference this issue if you see fit.

github-actions[bot] avatar May 08 '23 00:05 github-actions[bot]