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

pkg fails to respect the `exports` fields #56

Closed derevnjuk closed 4 months ago

derevnjuk commented 4 months ago

What version of pkg are you using?

5.11.5

What version of Node.js are you using?

18.20.2

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?

node18

Describe the Bug

When attempting to package a Node.js application that uses the axios library (version 1.6.3), the pkg command fails to create functional single executable and issues warnings about not being able to generate bytecode for axios files.

Steps to Reproduce:

  1. Include axios version 1.6.3 in your project's dependencies.
  2. Utilize axios in your application.
  3. Run the packaging command: pkg . -t node18-macos-x64.

Similar tickets: vercel/pkg#2042 and vercel/pkg#1873

Expected Behavior

The application should be successfully packaged into binaries as specified in the main and exports fields the package.json

To Reproduce

The single executables are created, and the following warnings are displayed:

> Warning Failed to make bytecode node18-x64 for file /snapshot/bright-cli/node_modules/axios/lib/platform/node/classes/URLSearchParams.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/bright-cli/node_modules/axios/lib/helpers/throttle.js

However, while running the single executable, it prints the following error message:

Error: Cannot find module '/snapshot/bright-cli/node_modules/axios/dist/node/axios.cjs'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:1171:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:1159:15)
    at resolveExports (node:internal/modules/cjs/loader:584:14)
    at Module._findPath (node:internal/modules/cjs/loader:658:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1120:27)
    at Function._resolveFilename (pkg/prelude/bootstrap.js:1955:46)
    at Module._load (node:internal/modules/cjs/loader:975:27)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at Module.require (pkg/prelude/bootstrap.js:1851:31)
    at require (node:internal/modules/helpers:177:18) {
  code: 'MODULE_NOT_FOUND',
  path: '/snapshot/bright-cli/node_modules/axios/package.json',
  pkg: true
}
robertsLando commented 4 months ago

This is a known bug, see #16

derevnjuk commented 4 months ago

@robertsLando I see. However, Axios, like many other dependencies, uses the exports field to declare multiple artifacts for both CommonJS and ESM. Adding support for ESM does not resolve the issue with loading CommonJS entries declared in the exports field.

robertsLando commented 4 months ago

see the comment: https://github.com/yao-pkg/pkg/issues/16#issuecomment-1945486658

derevnjuk commented 4 months ago

@robertsLando I see. Thanks 🙏🏾