You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 by pkg. However, node main.js runs without any errors.
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\snapshot\my-yao-pkg\node_modules\@apollo\server\dist\cjs\index.js from C:\snapshot\my-yao-pkg\main.cjs not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in C:\snapshot\my-yao-pkg\node_modules\@apollo\server\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
at Module.require (pkg/prelude/bootstrap.js:1851:31)
at Object.<anonymous> (C:\snapshot\my-yao-pkg\main.cjs)
at Module._compile (pkg/prelude/bootstrap.js:1930:22) {
code: 'ERR_REQUIRE_ESM'
}
Expected Behavior
no errors
To Reproduce
create main.js file and npm install @apollo/server
@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.
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.
@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".
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.
Activity
robertsLando commentedon Dec 8, 2023
ESM are not supported
shouchechengadv commentedon Dec 8, 2023
@apollo\serversupport 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.shouchechengadv commentedon Dec 22, 2023
I found a simple solution to solve this problem, which is to add
**/package.jsonto thepkg assets.The reason is that when Node.js parses files with the
.jsextension, it looks for the nearestpackage.jsonfile to determine whether it is ESM or CommonJS.Although
@apollo/serverhas set@apollo\server\dist\cjs\package.jsonfor@apollo\server\dist\cjs\index.js,pkgdoes not include it in the snapshot.As a result, it reads
@apollo\server\package.json, which has"type": "module", causing theERR_REQUIRE_ESMerror.I'm not sure why
node18-win-x64doesn't encounter the same problem.robertsLando commentedon Dec 22, 2023
Could you track this bug in the code and submit a PR to fix it? Could be an error with the resolver
davidmwhynot commentedon Feb 1, 2024
@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".
robertsLando commentedon Feb 2, 2024
@davidmwhynot Did you tried with latest pkg version?
Infern1 commentedon Feb 12, 2024
This was also a fix for using tslog https://github.com/fullstack-build/tslog
Tried with pkg
5.11.3robertsLando commentedon Feb 12, 2024
The problem I think is due to
resolvemodule not supporting ESM modules, related issue: browserify/resolve#222We are actually using it in lib/wolker.ts
followfunction:pkg/lib/follow.ts
Line 37 in 2fb1e12
I tried to replace it long time ago but without success
robertsLando commentedon Feb 15, 2024
Dup of #16 (comment)