Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Bun build command


Bun build command


The `bun build` command is a key feature of Bun, a modern JavaScript runtime that allows developers to compile and bundle their applications into standalone executables. This command supports various options for building, including cross-compilation for different operating systems and architectures.

Basic Usage

To build a JavaScript or TypeScript file into an executable, you can use the following syntax:


bun build --compile ./path/to/your/app.ts --outfile yourapp

This command compiles the specified file (`app.ts`) into an executable named `yourapp`. The `--compile` flag indicates that you want to generate a standalone binary.

Cross-Compilation

Bun also supports cross-compilation, allowing you to create executables for different platforms. You can specify the target platform using the `--target` option. For example:

- To build for Linux x64:


  bun build --compile --target=bun-linux-x64 ./index.ts --outfile myapp
  

- To build for Windows x64:


  bun build --compile --target=bun-windows-x64 ./path/to/my/app.ts --outfile myapp
  

- For macOS x64:


  bun build --compile --target=bun-darwin-x64 ./path/to/my/app.ts --outfile myapp
  

Supported Targets

Bun supports various targets, which include:

| Target | Operating System | Architecture |
|----------------------|------------------|--------------|
| bun-linux-x64 | Linux | x64 |
| bun-linux-arm64 | Linux | arm64 |
| bun-windows-x64 | Windows | x64 |
| bun-darwin-x64 | macOS | x64 |
| bun-darwin-arm64 | macOS | arm64 |

Output and Artifacts

The output of the `bun build` command includes all imported modules and dependencies bundled into the executable, along with the Bun runtime. This means that the generated binary can run independently of the Bun environment being installed on the target machine[1][3][6].

In summary, the `bun build` command is a powerful tool for developers looking to create efficient and portable applications using Bun, enabling easy deployment across various platforms.

Citations:
[1] https://bun.sh/blog/bun-bundler
[2] https://answers.netlify.com/t/install-and-build-with-bun/101727
[3] https://bun.sh/docs/bundler/executables
[4] https://shaneosullivan.wordpress.com/2023/05/17/using-bun-js-as-a-bundler/
[5] https://community.render.com/t/support-bun-for-build-steps-and-dependency-management/20222
[6] https://developer.mamezou-tech.com/en/blogs/2024/05/20/bun-cross-compile/
[7] https://stackoverflow.com/questions/72988142/what-is-the-build-command-for-react-app-created-with-bun-js
[8] https://github.com/oven-sh/bun/discussions/2936