Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Bun bundle executable


Bun bundle executable


Bun is a modern JavaScript runtime that allows developers to create standalone executable files from their JavaScript or TypeScript projects. This capability simplifies deployment by bundling the Bun runtime along with the project's source files into a single executable, which can be run without requiring the Bun environment to be installed on the target machine.

Creating Standalone Executables

To generate a standalone executable with Bun, you can use the `bun build` command with the `--compile` flag. This command compiles your entry file (e.g., a TypeScript or JavaScript file) into an executable binary. The basic syntax is as follows:


bun build ./yourfile.ts --compile --outfile yourapp

This command will produce an executable named `yourapp`, which includes all imported files and packages, as well as a copy of the Bun runtime. The generated executable can be run directly:


./yourapp

Cross-Compilation

Bun also supports cross-compiling executables for different platforms using the `--target` option. For example, to build an executable for Windows, you would use:


bun build --compile --target=bun-windows-x64 ./yourfile.ts --outfile yourapp.exe

This feature allows developers to create binaries for various architectures and operating systems from a single development environment[2][3][4].

Size and Performance Considerations

The size of the generated executables can be significant, often exceeding 50MB for simple applications. While there are efforts to reduce this size through minification and optimization techniques, users have noted that the initial size can be a drawback compared to other solutions like Go or Deno, which are known for producing smaller binaries[1][2][6].

Additional Tools

For those looking for alternative methods to package their Bun applications, tools like `bkg` are available. This CLI tool can generate self-sufficient binaries from Bun code for multiple platforms while potentially reducing binary size by optimizing the packaging process[5].

In summary, Bun's ability to create standalone executables enhances its usability for developers looking to streamline deployment processes while maintaining performance across various platforms.

Citations:
[1] https://github.com/oven-sh/bun/issues/441
[2] https://bun.sh/docs/bundler/executables
[3] https://developer.mamezou-tech.com/en/blogs/2024/05/20/bun-cross-compile/
[4] https://bun.sh/docs/bundler
[5] https://github.com/theseyan/bkg
[6] https://news.ycombinator.com/item?id=35965782
[7] https://stackoverflow.com/questions/78703717/getting-a-referenceerror-when-trying-to-bundle-a-single-typescript-file-with-bun
[8] https://www.reddit.com/r/javascript/comments/1adwwht/an_example_of_how_to_use_nodes_experimental/