-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESM Modules not supported #16
Comments
What's the error you see on console when you run the app? I have axios too in my project and using that solution works for me |
In my case I get these errors to begin with:
then it continues with the same errors as before:
and so on... |
I suggest to try pre-building your application using es-build and then pkg against the single file bundle. Otherwise if you are able to spot the error feel free to submit a PR |
Let's use this issue for all the others. Seems that starting from nodejs 18.19.0 and for all nodejs 20+ versions some modules that support both esm and cjs are not bundled correctly. This is IMO due to resolve package that is not able to correctly parse package.json Let's use this issue as a tracker for such issues instead of having multiple open ones. The error will look like:
Alternative solutionsBelow some solutions I tried and other possible working ones Using
|
The only approach that finally worked for me was to use esbuild, but it was still a huge undertaking, as my project is quite large an complex... |
@manast tried |
This may fix all those issues: nodejs/node#51977 (comment) Ref: https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/ |
That's really exciting news, but I've already given up on using pkg with ESM because I wanted to support the import syntax in the VM and use the latest packages. It looks like I can go back to pkg now? |
@luckyyyyy not yet there is still not an available nodejs lts version with that and I need to play with it a bit |
My issueI've been trying for a while to resolve this issue, as I'm stuck behind without the option to add some modules that shipped with the My
|
While still not an ideal solution, I had similar issues upgrading from Node 18 to Node 20 and was able to get basic support for a pkg built executable with native node modules, embedded C++ binaries, and other JS dist assets bundled and functioning with minimal effort using #10 (comment) I still agree that for existing known mitigations, refactoring to use ESBuild may prove to be a better long term tactic. |
Just wanted to let you all know that we have a successful usage of esbuild with PKG and node 20 in https://github.com/MatrixAI/Polykey-CLI - it even has native binaries involved and nix involved (all in flake.nix). |
Thanks for sharing 🙏 Any description of how? |
Hey there, following the post of @CMCDragonkai I also tried to move our CLI to use ESM with esbuild (on node18). I could manage to get the full flow working, including with native librairies. The build script is pretty similar to the one in Polykey: https://github.com/Dashlane/dashlane-cli/blob/master/scripts/build.js I could use One caveat I encountered is that esbuild is not transpiling the dynamic imports If you have time to take a look, I'm really looking for feedback :) |
Check this evanw/esbuild#2651. |
And this evanw/esbuild#700 regarding dynamic imports. |
@robertsLando nodejs/node#51977 mentions the lts pr nodejs/node#54447 and it seems to soon be in v20.17.0, which is the next lts version. |
v20.17.0 is now released and has |
@robertsLando, how much of a lift would it be to make the v20.17.0 LTS available to pkg-fetch? I would love to start testing this flag within PKG's context. |
@SagePacheco long story, when I started this fork I know it would have been a pain but didn't thought like this (reason why most of people maintaining this project have give up at a certain point) I spent weeks on this PR in order to fix compatibility with node 20.14 and ended up being able to make everything working except for windows that actually builds but fails on runtime (check conversation if you are curious). After many thoughts on this I decided to just merge the PR so users are able to use latest nodejs versions with linux and mac (hope someone will help to fix windows one once issues start) then I discovered that GH removed macos-11 from runners, so I bumped macos runners to use macos-13 and guess what? now mac build are not working and the sad thing is that they just hang without any output. I'm a bit stuck now as the free time I have to invest in this is very limited and the tests here require long waiting time. I hope someone could help with this |
Just a quick update here. I finally managed to make all build working and new release is coming with latest nodejs 18 and 20.17.0 support 🎉 Just need to wait for this to finish now Please consider sending some support here: https://github.com/sponsors/robertsLando 🙏🏼 ❤️ |
pkg 5.13.0 is ou now with nodejs 20.17.0 and 18.20.4 support 🎉 |
Cheers, @robertsLando! Thanks for working that out. It is much appreciated for you to keep this library alive after Vercel. PKG is hugely important to my organization and will continue to be that way until SEA is worth anything, so let me see what I can talk my leadership into for sponsorship. In regard to the The good: The bad: I have even tried to bundle these "missing files" under assets / scripts to force inclusion in the virtual file system and then validated its existence at the expected path using |
@SagePacheco thanks for looking at that, was on my TODOs list but no idea when and if I would ever found the time to look at it. ATM the best way (it's what I do on my projects) is to pre-compile everything with esbuild and I have to say that's very powerful as you also reduce binary size a lot |
Seems the majority of problems solved with Here is a working docker file which I use for my project: FROM node:20.17.0-alpine as build
# prepare
RUN mkdir -p app
WORKDIR /app
# install dependencies
RUN npm i -g @yao-pkg/pkg@5.15.0
COPY . .
RUN npm i
# build binary
RUN pkg -C GZip --options experimental-require-module --targets node20-alpine -o app-alpine .
FROM node:20.17.0-alpine as release
# for alpine
RUN apk --no-cache add ca-certificates && apk --update --no-cache add curl tzdata bash
WORKDIR /app
COPY --from=build /app/app-alpine ./
# run
# CMD ["bash", "-c", "./app-alpine"]
ENTRYPOINT ["/app/app-alpine"] |
Thanks for the report @paulish ! |
I have a CommonJS application that depends on
|
Someone wants to give this a try? #110 🚀 |
@robertsLando is there any workaround to precompile esm modules with top-level-await and then use pgk? |
What version of pkg are you using?
5.11.1
What version of Node.js are you using?
16.19.1
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?
node16-macos-x64
Describe the Bug
This issue is mentioned in the original package, but the package is archived, then I found a solution in issue's comment:
vercel#1936
But it still does not work
Expected Behavior
Compile app without axios (lastest version) error
To Reproduce
A Project that include the lastest version of axios (e.g: v1.6.4)
The text was updated successfully, but these errors were encountered: