Skip to main content

Deploy Bun Apps

Bun is a recently popular Node alternative for back-end Javascript runtime environment. Like Node, you don't need to compile your code beforehand.

There is no default bun compiler at system level. You have to install it first through deployment scripts.

warning

Serving Bun apps requires to be run via GLS, that's mean you have to listen from given PORT env/args. Bun.serve is already doing this if you don't set the port explicitly.

Example

The deployment script below installs the latest bun compiler and writes app.ts and serves that.

https://github.com/domcloud/recipes/blob/master/bun.yml
source: clear
features:
- bun latest
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT bunfix bun app.ts
commands:
- filename: app.ts
content: |
const html_text = `
<!DOCTYPE html>
<html>
<head>
<title>Bun App</title>
<link rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css">
</head>
<body class="p-5 text-center">
<p><img src="//images.unsplash.com/photo-1465153690352-10c1b29577f8?fit=crop&w=200&h=200"
class="img-fluid rounded-circle"></p>
<h1 class="mb-3">Hello, world!</h1>
<p>Serving from Bun version ${Bun.version}</p>
<p class="text-muted">DOM Cloud</p>
</body>
</html>
`
const server = Bun.serve({
fetch(request) {
return new Response(html_text, {
headers: { "Content-Type": "text/html" },
});
},
});

Note three things:

  • Bun.serve already set the port arg from PORT env by default
  • bunfix bun app.ts is used to run Bun with a workaround of an ongoing issue
  • This bunfix proxies request to the bun app, this proxy support HTTP and Websocket

For existing bun apps, use this deployment script. Please adjust relevant fields such as the startup file (change app.ts) and how $PORT is delivered (via env PORT=$PORT bunfix bun ...).

https://github.com/domcloud/recipes/blob/master/bun-gls.yml
features:
- bun latest
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT bunfix bun app.ts

Use other Bun versions

To switch Bun version use feature syntax like bun latest, bun beta, bun 1.0.0, etc. Check your current Bun version using bun --version in SSH.

features:
- bun 1.0.0

Bun install scripts is powered by webi.

App Management

Your app do not restarted automatically after file changes. To restart, run restart via SSH.

Environment variables can be set either using NGINX's env_var_list or ~/.bashrc. Usually your language framework also reads .env files.

See NGINX and App Daemon for more information about NGINX and App managements including restarting, environment variables, and other global limitations.

App Logging

You can see app log from Check -> Check Process Logs tab. Only startup problems displayed in the browser.

Bun can show runtime errors in browser by setting app_env: development in the passenger config of NGINX.

It's recommended to use a proper logging mechanism such as Pino or Winston then write it to a log file, or any other solution that suits you.

NGINX errors and traffic logs can be examined via Webmin or Check -> Check Process Logs tab.