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 seems to ignore package.json `pkg` config #64

Closed adamkdean closed 3 months ago

adamkdean commented 3 months ago

What version of pkg are you using?

5.11.5

What version of Node.js are you using?

20.11.0

What operating system are you using?

macOS

What CPU architecture are you using?

Apple M3 Max (arm64)

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

node20

Describe the Bug

I'm trying to get pkg to build with sharp but running into issues.

// package.json

{
  "name": "pkg-test",
  "version": "0.0.0",
  "scripts": {
    "start": "node src/test.js",
    "build": "pkg --debug --out-dir dist src/test.js",
    "test": "./dist/test"
  },
  "dependencies": {
    "sharp": "^0.33.3"
  },
  "bin": "src/test.js",
  "pkg": {
    "assets": [
      "node_modules/sharp/build/Release/**/*",
      "node_modules/sharp/vendor/lib/**/*",
      "bin/**/*"
    ],
    "targets": [
      "node20-macos",
      "node18-macos"
    ]
  }
}

Not only does the install for sharp not seem to work (separate issue I think) but the targets are ignored.

> pkg@5.11.5
> Targets not specified. Assuming:
  node20-linux-arm64, node20-macos-arm64, node20-win-arm64
> [debug] Bytecode of <snip>pkg-test/src/test.js is added to queue.
> [debug] Stat info of <snip>pkg-test/src/test.js is added to queue.
> [debug] Content of <snip>pkg-test/node_modules/sharp/package.json is added to queue. It was required from <snip>pkg-test/src/test.js
> [debug] Bytecode of <snip>pkg-test/node_modules/sharp/lib/index.js is added to queue. It was required from <snip>pkg-test/src/test.js
> [debug] Directory <snip>pkg-test/src is added to queue.
> [debug] Stat info of <snip>pkg-test/node_modules/sharp/package.json is added to queue.
> Warning Cannot include directory %1 into executable.
  The directory must be distributed with executable as %2.
  %1: node_modules/sharp/build/Release
  %2: path-to-executable/sharp/build/Release
> Warning Cannot include directory %1 into executable.
  The directory must be distributed with executable as %2.
  %1: node_modules/sharp/vendor/lib
  %2: path-to-executable/sharp/vendor/lib

It almost seems like targets are assume prior to package.json being read?

(Or I could just be doing something wrong!)

Expected Behavior

Pkg should read the package.json file and parse the targets.

To Reproduce

Use:

{
  "name": "pkg-test",
  "version": "0.0.0",
  "scripts": {
    "start": "node src/test.js",
    "build": "pkg --debug --out-dir dist src/test.js",
    "test": "./dist/test"
  },
  "bin": "src/test.js",
  "pkg": {
    "targets": [
      "node20-macos-arm64",
      "node18-macos-arm64"
    ]
  }
}

The src/test.js just has a console.log in it, nothing more. The targets can be changed for others but makes no difference.

adamkdean commented 3 months ago

Just as I post the ticket after hammering my head at this for days, I realise, you use pkg . to make use of the package.json config. Wasn't entirely clear to me. Perhaps we could improve the README? (Happy to do it.)

robertsLando commented 3 months ago

@adamkdean Feel free to open a PR