Closed shouchechengadv closed 6 months ago
ESM are not supported
@apollo\server
support ESM and CommonJS.
The error message indicates that the require JS file is CommonJS format. (@apollo\server\dist\cjs\index.js
)
The same code runs without any errors in node18-win-x64
.
I found a simple solution to solve this problem, which is to add **/package.json
to the pkg assets
.
// package.json
{
"pkg": {
"assets": ["**/package.json"]
}
}
The reason is that when Node.js parses files with the .js
extension, it looks for the nearest package.json
file to determine whether it is ESM or CommonJS.
Although @apollo/server
has set @apollo\server\dist\cjs\package.json
for @apollo\server\dist\cjs\index.js
, pkg
does not include it in the snapshot.
// @apollo\server\dist\cjs\package.json
{"type":"commonjs"}
As a result, it reads @apollo\server\package.json
, which has "type": "module"
, causing the ERR_REQUIRE_ESM
error.
I'm not sure why node18-win-x64
doesn't encounter the same problem.
Could you track this bug in the code and submit a PR to fix it? Could be an error with the resolver
@robertsLando I've not contributed to this project in the past but we ran into this issue today and I'd like to do what you suggested and track this down. However, I'm unsure what you're referring to when you say "resolver".
@davidmwhynot Did you tried with latest pkg version?
I found a simple solution to solve this problem, which is to add
**/package.json
to thepkg assets
.// package.json { "pkg": { "assets": ["**/package.json"] } }
The reason is that when Node.js parses files with the
.js
extension, it looks for the nearestpackage.json
file to determine whether it is ESM or CommonJS.Although
@apollo/server
has set@apollo\server\dist\cjs\package.json
for@apollo\server\dist\cjs\index.js
,pkg
does not include it in the snapshot.// @apollo\server\dist\cjs\package.json {"type":"commonjs"}
As a result, it reads
@apollo\server\package.json
, which has"type": "module"
, causing theERR_REQUIRE_ESM
error.I'm not sure why
node18-win-x64
doesn't encounter the same problem.
This was also a fix for using tslog https://github.com/fullstack-build/tslog
Tried with pkg 5.11.3
The problem I think is due to resolve
module not supporting ESM modules, related issue: https://github.com/browserify/resolve/issues/222
We are actually using it in lib/wolker.ts follow
function: https://github.com/yao-pkg/pkg/blob/2fb1e1264a685050d3ad8100ce3c8a0b4d013381/lib/follow.ts#L37.
I tried to replace it long time ago but without success
What version of pkg are you using?
5.11.0
What version of Node.js are you using?
20.10.0
What operating system are you using?
Windows, Ubuntu 22.04
What CPU architecture are you using?
x86_64
What Node versions, OSs and CPU architectures are you building for?
node20-win-x64, node20-linux-x64
Describe the Bug
After
require('@apollo/server')
, an error is thrown when executing the executable created bypkg
. However,node main.js
runs without any errors.Expected Behavior
no errors
To Reproduce
npm install @apollo/server
npx pkg main.js --target node20-win-x64
main.exe