Bun application runtime
Overview
Bun is a JavaScript runtime and a package manager, which runs TypeScript without a build. On a Node.js application, Clever Cloud installs your dependencies and starts your project with Bun when it finds a bun.lock file. Bun includes clients for SQL databases, S3, and Redis, which work with several add-ons of Clever Cloud, without any dependency to add.
Create your Bun application
A Bun application is a Node.js application. To create it, use the Clever Cloud Console or Clever Tools:
clever create --type nodeThe getting started guide describes each step of the creation of an application.
Configure your Bun application
Mandatory needs
A Bun application needs:
- A
bun.lockfile, orCC_NODE_BUILD_TOOL=bun, so that Clever Cloud uses Bun - A
package.jsonfile, with a start script or amainfield, to start your application - A web server listening on
0.0.0.0, on the port inPORT
bun install writes the bun.lock file, which sets the exact version of each dependency: commit it. Clever Cloud checks the yarn.lock and pnpm-lock.yaml files first, so remove them from a project that moves to Bun.
Clever Cloud doesn’t detect the binary bun.lockb file of Bun releases before 1.2, and then installs your dependencies with npm. Convert it with bun install --save-text-lockfile, or set CC_NODE_BUILD_TOOL=bun to use Bun anyway.
For instance, a minimal Hono application, in TypeScript:
{
"name": "hello-bun",
"type": "module",
"scripts": {
"start": "bun server.ts"
},
"dependencies": {
"hono": "^4.13.20"
},
"devDependencies": {
"@types/bun": "^1.4.5"
}
}import { Hono } from 'hono';
const app = new Hono();
app.get('/', (c) => c.text('Hello from Bun on Clever Cloud'));
Bun.serve({
hostname: '0.0.0.0',
port: Number(Bun.env.PORT ?? 8080),
fetch: app.fetch,
});To try it locally, run bun install, then bun start: it listens on port 8080.
Build phase
On a new build, Clever Cloud runs the pre-build hook, then installs the dependencies of your project, without its devDependencies:
bun install --omit dev --no-progressClever Cloud skips them whatever the value of NODE_ENV, unlike npm. To install them, such as for a build tool, set CC_NODE_DEV_DEPENDENCIES=install: Clever Cloud then runs bun install --no-progress.
Bun runs TypeScript files directly. To build your application, such as to bundle a frontend, run your build script in the post-build hook:
clever env set CC_POST_BUILD_HOOK "bun run build"A hook runs from the root of your repository: with CC_APP_FOLDER, start it with cd to this folder.
Bun version
Clever Cloud uses Bun 1.4.5 by default, the version of its images. To set another one, add Bun to a mise.toml file in your project:
[tools]
bun = "1.4.4"Mise installs this version before the build: it then installs your dependencies and runs your application.
Start command
Clever Cloud starts your application from the folder of your project, with the first of these commands that applies:
CC_RUN_COMMAND, when you set itbun start, which runs the start script of yourpackage.jsonfilebunand the file of themainfield, such asbun server.ts
Built-in clients for Clever Cloud add-ons
These clients of Bun connect to add-ons from the variables that they set:
redis: Redis and Materia KV, withREDIS_URL, which it reads by defaultS3Client: Cellar, withCELLAR_ADDON_*and an existing bucketSQL: MySQL and PostgreSQL, withMYSQL_ADDON_URIorPOSTGRESQL_ADDON_URI
For instance, with a Redis, a PostgreSQL, and a Cellar add-on linked to your application:
import { redis, S3Client, SQL } from 'bun';
const db = new SQL(Bun.env.POSTGRESQL_ADDON_URI);
const cellar = new S3Client({
accessKeyId: Bun.env.CELLAR_ADDON_KEY_ID,
secretAccessKey: Bun.env.CELLAR_ADDON_KEY_SECRET,
endpoint: `https://${Bun.env.CELLAR_ADDON_HOST}`,
bucket: 'my-bucket',
});
const visits = await redis.incr('visits');
const [{ now }] = await db`SELECT now()`;
await cellar.file('hello.txt').write(`Visit number ${visits}, at ${now}`);The Bun add-ons examples show how to use other Clever Cloud add-ons with Bun.
Caches
After a successful build of an application, Clever Cloud keeps a build cache of the commit. Restarting the application without a code change reuses it and skips the build. To build again from scratch, restart without the cache (rebuild):
clever restart --without-cacheWith CC_CACHE_DEPENDENCIES=true, Clever Cloud also keeps the packages that Bun downloads between commits, in ~/.bun/install/cache. If you move this folder with the BUN_INSTALL_CACHE_DIR variable of Bun, Clever Cloud keeps this folder instead.
Bun applications as Clever Tasks
A Bun application can run as a Clever Task, such as a job or a migration. Clever Cloud installs its dependencies with Bun, then runs CC_RUN_COMMAND from the folder of your project, instead of starting a web server. The task stops when this command ends.
Configure Tasks from the Information panel in the Console or with Clever Tools:
clever create --type node --task "bun migrate.ts"
clever deploy # or clever restart if there is no code change