bun
bun copied to clipboard
Bundle a standalone executable
I was wondering if there was a way to bundle your project together with bun into a standalone executable. Much like with nexe
or pkg
for node.
Given that bun is not widespread, but has the edge on performance, this could reduce a great deal of friction in the deployment of projects.
+1
pkg creates a 30-40MB executable for a simple console.log.
Would be hugely awesome if we could create smaller executables.
Size of executables from deno compile
are also pretty big, smaller executables sound amazing!
sounds great
@SrBrahma @felix-schindler AFAIK pkg and deno compile
(which is still an experimental feature for deno, I believe) create executables by bundling the node/deno runtime (js vm and more) with your scripts and assets, hence large file sizes (even for console.log("hi")
). Also, AFAIK, all major javascript engines are virtual machines, they don't produce executable files; so if you want to create an executable that is comparable in size/performance with something, say Zig, would produce, you would need to create a whole new JS engine (compiler), and I doubt this is a goal of Bun, but feel free to correct me :).
@not-pope Doubt anybody expects that. But perhaps the bundled jscore runtime can be stripped of any isolatable components that the project is not using. For example multiprocessing feature set, which is commonly not being used.
I was wondering if there was a way to bundle your project together with bun into a standalone executable. Much like with
nexe
orpkg
for node.Given that bun is not widespread, but has the edge on performance, this could reduce a great deal of friction in the deployment of projects.
This is exactly what I also had in mind when I saw this. I have Nodejs deployments on Windows and it'd be a deal breaker since the environment doesn't allow for WSL to be run there. The best way would be to compile to exe, and since bun seems to compile code anyway, it'd be great if it can compile to executables straight away. I am not concerned by the size here since Nodejs, in this instance will be taking space anyway... The thing I'm interested is to see if it can be used to make standalone Executables for use in systems where it may not otherwise run properly, or just where we would use, say PKG to create executables.
Highly wish for this. For me this is the edge Deno has on bun. if bun also gets the ability to package the entire project as a single executable, it will make it much easier to make the decision to use bun instead, as bun seems to do everything better than Deno anyway.
Like nw.js, bun executable can be concated with .bun bundle file.
You can run following command on Windows to achieve this: copy /b nw.exe+package.nw app.exe
or following command on Linux: cat nw package.nw > app && chmod +x app
https://docs.nwjs.io/en/latest/For%20Users/Package%20and%20Distribute/
The (main) point here is not the executable size or the ability to "compile" javascript into machine code. The point here is the ability to create a single executable which "bundles" both bun and a project's js files, allowing easier distribution.
Thanks to Dom (on Bun's Discord channel), that pointed me to github.com/theseyan/bkg.
The @theseyan's strategy on bkg is very clean and it may be interesting in bring it into Bun itself in the near future.
plans according to jarred is to have something like this in v0.8
related issue: #2541 inquiring about how the bun binary can be shrunk down (seems like it can't be shrunk super far)
Available in 0.6.0, via the bun build --compile :tada:
@paperdave so amazing! Thanks for it!
Available in 0.6.0, via the bun build --compile 🎉
Does the actual source files gets transpiled to any other form in this compile process or they get bundled in executable as it is?
Available in 0.6.0, via the bun build --compile 🎉
Does the actual source files gets transpiled to any other form in this compile process or they get bundled in executable as it is?
it is like running bun build
without --compile
, then it takes that code and combines it with bun so it runs such code on startup.
pkg creates a 30-40MB executable for a simple console.log.
Would be hugely awesome if we could create smaller executables.
Can anyone comment on how package size compares?
@Slapbox, here is a table with binary sizes on Linux, using bun build --compile ...
as well as bkg
and pkg
(latest nodejs LTS, v18).
The js source code is just a console.log('Hello World')
Command | Binary size | xz / gz / zip compressed | cold startup | warm startup | peak memory usage (binary) |
---|---|---|---|---|---|
bun build ./index.js --compile --outfile hello |
94M | 23M / 35M / 33M | 0.025s | 0.025s | 32.9M |
bkg -o hello . |
42M | 30M / 35M / 34M | 5.430s | 0.029s | 36.4M |
pkg -t node18-linux index.js |
45M | 13M / 18M / 17M | 0.115s | 0.116s | 38.5M |
My takeaway is:
-
bun build --compile ...
produces a bigger binary, but provides the fastest startup time (4.6x faster than nodejs) -
bkg
works similarly aspkg
, but the extra compression compared to nativebun build
means slower startup time on the first execution, which is undesired on serverless environments (bkg's readme already mentions it) - bun's binary generation is pretty new, and there is a lot of opportunities for improvements, like reduced runtime size by removing unused features (like wasm)