yao-pkg / pkg

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

Module present in the snapshot cannot be found #125

Open swanux opened 2 days ago

swanux commented 2 days ago

What version of pkg are you using?

6.1.0

What version of Node.js are you using?

20

What operating system are you using?

Fedora 41

What CPU architecture are you using?

x86_64

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

node20-linux-x64, node20-windows-x64

Describe the Bug

Calling playwright through the binary (node_modules/.bin), the same way as it used to work with the original vercel/pkg, it throws the following error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/snapshot/autotest/node_modules/playwright/lib/transform/esmLoader.js' imported from /snapshot/autotest/node_modules/playwright/lib/common/esmLoaderHost.js
    at finalizeResolution (node:internal/modules/esm/resolve:265:11)
    at moduleResolve (node:internal/modules/esm/resolve:933:10)
    at defaultResolve (node:internal/modules/esm/resolve:1169:11)
    at nextResolve (node:internal/modules/esm/hooks:868:28)
    at Hooks.resolve (node:internal/modules/esm/hooks:306:30)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:503:35)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:472:34)
    at Hooks.register (node:internal/modules/esm/hooks:167:34)
    at MessagePort.handleMessage (node:internal/modules/esm/worker:196:24) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///snapshot/autotest/node_modules/playwright/lib/transform/esmLoader.js'
}

While the following code (from within the compiled program): console.log(fs.readdirSync("/snapshot/autotest/node_modules/playwright/lib/transform"));

Gives the following result:

[
  'babelBundle.js',
  'babelBundleImpl.js',
  'compilationCache.js',
  'esmLoader.js',
  'esmUtils.js',
  'portTransport.js',
  'transform.js'
]

Indicating that the required file is clearly present in the snapshot.

Expected Behavior

The module import should be successful.

To Reproduce

Have the following config for pkg in your package.json:

"pkg": {
    "assets": [
      "node_modules/@playwright",
      "node_modules/playwright",
      "node_modules/playwright-core",
      "node_modules/.bin/playwright"
    ],
    "targets": [
      "node20-linux-x64",
      "node20-windows-x64"
    ]

Try to call playwright test from within the compiled code, according to this:

const playwright_bin = path.join(__dirname, "../node_modules/.bin/playwright");
...
const node = spawn("node", [playwright_bin, "test"]);
robertsLando commented 2 days ago

Try to check https://github.com/yao-pkg/pkg#troubleshooting and then if nothing works go to last advanced section to create a debug binary and lunch it to see if you can gain some more informations