Programming
Bun JS: All-in-One Toolkit JavaScript & TypeScript 2026
Daftar isi
- Apa Itu Bun JS dan Kenapa Kamu Perlu Peduli
- Latar Belakang: Masalah Toolchain JavaScript yang Bercabang
- Kenapa Bun Beda
- Spesifikasi Teknis Singkat
- Step 1: Prasyarat dan Persiapan Sistem
- Sistem Operasi yang Didukung
- Cek CPU Support
- Kenapa Step Ini Penting?
- Step 2: Instalasi Bun JS
- Metode 1: Install Script (macOS & Linux) — Direkomendasikan
- Metode 2: Homebrew (macOS)
- Metode 3: npm (Semua Platform)
- Metode 4: Windows (PowerShell)
- Metode 5: Docker
- Verifikasi Instalasi
- Troubleshooting: bun: command not found
- Step 3: Setup Project Pertama dengan Bun
- Inisialisasi Project
- Struktur File yang Dihasilkan
- Jalanin File Pertama
- Kenapa Ini Penting?
- Step 4: Menggunakan Bun sebagai Package Manager
- Install Semua Dependency
- Tambah Dependency Baru
- Hapus Dependency
- Lockfile: bun.lockb
- Install dengan Frozen Lockfile (CI/CD)
- Workspaces dan Monorepo
- Studi Kasus: Kecepatan Install di Project Nyata
- Step 5: Bun sebagai JavaScript Runtime
- Jalanin File JavaScript/TypeScript
- Kompatibilitas Node.js
- ESM dan CommonJS Bersamaan
- Environment Variables
- Hot Reloading
- Step 6: Membangun HTTP Server dengan Bun.serve()
- Server Sederhana
- Routing dengan routes Property
- WebSocket Server
- Cookies API
- Kenapa Pakai Bun.serve()?
- Step 7: Database Built-in — SQLite, PostgreSQL, dan Redis
- SQLite (bun:sqlite)
- PostgreSQL dengan sql Tag
- Redis Client
- Perbandingan: Database di Bun vs Node.js
- Step 8: Test Runner — Bun Test
- Struktur Test File
- Menulis Test
- Menjalankan Test
- Snapshot Testing
- Konfigurasi Test di bunfig.toml
- Studi Kasus: Migrasi dari Jest ke Bun Test
- Step 9: Bundler — bun build
- Bundle untuk Browser
- Bundle untuk Node.js
- Bundle dengan Minification
- Single-File Executable
- Bundle dengan HTML Entry Point
- Programmatic API
- Benchmark Bundling
- Step 10: File System dan Utility APIs
- Bun.file() — Baca File Cepat
- Bun.write() — Tulis File Cepat
- Bun.$ — Cross-Platform Shell
- Bun.password — Hashing Password
- Bun.hash — Hashing Cepat
- YAML Support
- FFI — Call Native C/C++ Code
- Perbandingan Bun vs Node.js vs Deno
- Rekomendasi per Use Case
- Frontend Development dengan Bun
- Setup React App
- Dev Server
- Serve Frontend + API Bersamaan
- Build untuk Production
- Konfigurasi Lanjutan: bunfig.toml
- TypeScript Config yang Direkomendasikan
- Deployment ke Production
- Setup di Server
- Build Command untuk CI/CD
- Run dengan PM2
- Run dengan systemd
- Branch Environments
- Error dan Troubleshooting Umum
- 1. bun: command not found Setelah Install
- 2. Illegal Instruction Error
- 3. bun install Fails dengan lockfile had changes
- 4. Package dengan Native Addon Tidak Bekerja
- 5. TypeScript Errors tentang Bun Globals
- 6. Port Already in Use
- 7. GLIBC... not found Error di Linux
- 8. Error EACCES Permission Denied
- Upgrade dan Manajemen Versi
- Upgrade ke Versi Terbaru
- Switch ke Canary Build
- Install Versi Spesifik
- Upgrade via Homebrew
- Upgrade via Scoop (Windows)
- Uninstall Bun
- Studi Kasus: Migrasi Full-Stack dari Node.js ke Bun
- Background
- Challenge
- Approach
- Implementation
- Results
- Key Learnings
- Tips dan Best Practices
- 1. Selalu Commit Lockfile
- 2. Pakai Bun.$ untuk Scripting
- 3. Manfaatkan Built-in APIs
- 4. Pakai --hot untuk Development
- 5. Cross-Compile untuk Distribusi
- 6. Monitor Memory di Production
- 7. Pakai bun.lockb di .gitignore? Jangan!
- Siapa yang Sebaiknya Pakai Bun — dan Siapa yang Sebaiknya Tunggu
- Bun Cocok Banget Buat:
- Mungkin Tunggu Dulu Kalau:
- Fitur Bun yang Sering Diabaikan tapi Sangat Berguna
- 1. Bun.semver — Compare Version Strings
- 2. Bun. Glob — File Pattern Matching
- 3. Bun.stringWidth — Calculate Terminal Display Width
- 4. Bun. CSRF — Generate dan Verify CSRF Tokens
- 5. Bun.secrets — Encrypted Secrets Storage
- 6. Bun.color — CSS Color Conversion
- Eksplorasi Lebih Lanjut
- Yang Perlu Di-watch Ke Depan
- Tips Migrasi dari Node.js ke Bun yang Minim Drama
- Mulai dari Hal Kecil
- Yang Sering Jadi Sumber Masalah
- Strategi Hybrid yang Aman
- Benchmarking Sendiri di Environment Teman-Teman
- Kesimpulan
Pernah nggak Teman-Teman ngerasa capek banget tiap kali mau mulai project JavaScript baru? Install dependency aja bisa makan waktu puluhan detik, nunggu bundling kelar sambil ngopi, dan setup TypeScript sampai pusing tujuh keliling. MUGHU sendiri dulu sempet bikunin tujuh tools berbeda cuma buat satu project kecil — Node.js buat jalanin server, npm buat install paket, Jest buat testing, esbuild buat bundling, ts-node buat TypeScript... capek banget.
Nah, di tahun 2026 ini, ada satu tool yang bikin hidup kita sebagai developer jadi jauh lebih ringkas: Bun JS. Bun adalah all-in-one toolkit untuk JavaScript dan TypeScript yang menggabungkan runtime, package manager, test runner, dan bundler dalam satu executable tunggal. Dibuat pertama kali oleh Jarred Sumner pada September 2021, Bun sekarang sudah mencapai versi 1.3.14 dan dipakai oleh tim-tim engineering di perusahaan besar seperti Midjourney, Railway, dan bahkan Claude Code.
Bun JS adalah JavaScript runtime, package manager, test runner, dan bundler yang digabung jadi satu tool tunggal, dirancang sebagai pengganti Node.js dengan dukungan TypeScript dan JSX bawaan tanpa konfigurasi tambahan.
Artikel ini bakal ngebahas tuntas dari instalasi sampai deployment — lengkap dengan studi kasus nyata, perbandingan dengan alternatif, tips troubleshooting, dan lesson learned yang MUGHU kumpulin dari pengalaman langsung pakai Bun di production.
Apa Itu Bun JS dan Kenapa Kamu Perlu Peduli
Latar Belakang: Masalah Toolchain JavaScript yang Bercabang
Dunia JavaScript punya masalah klasik: toolchain sprawl. Satu project tipikal butuh lima atau lebih tool terpisah cuma buat dari source code sampai server jalan. Tiap tool punya format konfigurasi sendiri, versi sendiri, dan permukaan error sendiri. Kalau satu tool rusak di CI, seluruh pipeline ikut macet.
MUGHU inget banget pengalaman satu project client di Jakarta. Tim kita butuh 45 detik cuma buat npm install di CI/CD pipeline. Ditambah 30 detik buat transpile TypeScript, 20 detik buat bundle dengan Webpack, dan 15 detik buat jalankan Jest. Total build time: hampir 2 menit setiap push. Untuk tim yang deploy beberapa kali sehari, itu cumulatif jadi jam-jam terbuang sia-sia.
Kenapa Bun Beda
Bun menyelesaikan masalah ini dengan pendekatan yang radikal: satu binary, satu toolchain. Bun ships sebagai single executable tanpa dependency, dan di dalamnya sudah ada:
-
Runtime — jalanin JavaScript dan TypeScript langsung, tanpa transpile step terpisah
-
Package Manager — install paket dari npm hingga 30x lebih cepat
-
Test Runner — compatible dengan Jest API, jalan 10-30x lebih cepat
-
Bundler — bundle untuk browser dan server dengan tree-shaking dan minification
Bun ditulis dalam Rust dan menggunakan JavaScriptCore (engine dari Safari/WebKit) sebagai JavaScript engine, bukan V8 seperti Node.js dan Deno. Pilihan ini bukan kebetulan — JavaScriptCore terkenal punya startup time yang jauh lebih cepat dari V8, yang artinya cold start Bun bisa 3-4x lebih cepat dari Node.js.
Untuk konteks serverless dan CLI tools, beda startup time ini sangat berpengaruh. Cold start yang biasanya 300ms di Node.js bisa turun ke bawah 50ms di Bun. Di skala ratusan atau ribuan invocations per hari, penghematan waktunya nyata banget.
Spesifikasi Teknis Singkat
Aspek | Detail |
|---|---|
Bahasa implementasi | Rust (runtime), JavaScriptCore (JS engine) |
Engine JavaScript | JavaScriptCore (dari WebKit/Safari) |
Versi stabil terbaru | 1.3.14 (Mei 2026) |
Platform didukung | macOS (x64 & ARM), Linux (x64 & ARM), Windows (x64 & ARM) |
Lisensi | MIT |
Ukuran binary | Single executable, dependency-free |
Module system | ESM + CommonJS (kompatibel keduanya) |
Step 1: Prasyarat dan Persiapan Sistem
Sebelum mulai, pastikan sistem Teman-Teman memenuhi syarat minimum. Bun dirancang ringan, tapi ada beberapa hal yang perlu dicek dulu.
Sistem Operasi yang Didukung
-
macOS — versi 13.0 atau lebih baru, baik Apple Silicon (M1/M2/M3) maupun Intel
-
Linux — kernel 5.6+ direkomendasikan (bisa jalan di kernel 3.10+ dengan degradasi), glibc 2.17+
-
Windows — Windows 10 versi 1809 atau lebih baru, x64 dan ARM64
Untuk pengguna Linux, pastikan package unzip terinstall karena installer Bun butuh tool itu untuk ekstrak binary. Cek dengan:
which unzip
Expected output:
/usr/bin/unzip
Kalau belum ada, install dulu:
# Ubuntu/Debian
sudo apt install unzip
# CentOS/RHEL/Fedora
sudo dnf install unzip
Cek CPU Support
Bun punya dua varian binary: standard (butuh AVX2) dan baseline (untuk CPU lebih lama). Installer otomatis mendeteksi CPU dan memilih varian yang tepat, tapi ada baiknya Teman-Teman tahu kondisi CPU sendiri.
# Linux
grep -o 'avx2' /proc/cpuinfo | head -1
# macOS
sysctl -a | grep machdep.cpu | grep AVX2
Kalau outputnya kosong, berarti CPU Teman-Teman nggak support AVX2 dan installer bakal otomatis pilih baseline build. Baseline build lebih lambat dari standard build, tapi tetap berfungsi normal.
Kenapa Step Ini Penting?
MUGHU pernah spend dua jam debugging kenapa Bun crash dengan "Illegal Instruction" di server lama. Setelah cek, ternyata CPU server-nya pre-Haswell dan nggak support AVX2. Installer seharusnya otomatis pilih baseline, tapi karena MUGHU install manual dari GitHub release, salah pilih varian. Jadi selalu cek dulu — ini nggak makan waktu tapi bisa hemat jam-jam debugging.
Step 2: Instalasi Bun JS
Bun bisa diinstall lewat beberapa cara. MUGHU bakal bahas yang paling umum dipakai.
Metode 1: Install Script (macOS & Linux) — Direkomendasikan
Cara paling straightforward adalah pakai official install script:
curl -fsSL https://bun.sh/install | bash
Expected output:
bun was installed successfully to ~/.bun/bin/bun
Added "~/.bun/bin" to $PATH in "~/.zshrc"
To get started, run:
source ~/.zshrc
Script ini otomatis:
-
Mendeteksi platform dan arsitektur CPU
-
Download binary yang sesuai dari GitHub releases
-
Ekstrak ke
~/.bun/bin/ -
Menambahkan PATH ke shell config (
.zshrc,.bashrc, atauconfig.fish)
Metode 2: Homebrew (macOS)

Baca juga OpenCode Free: Agen AI Coding Gratis dan Open Source
brew install oven-sh/bun/bun
Catatan: kalau install via Homebrew, upgrade juga harus via Homebrew (brew upgrade bun), bukan bun upgrade. MUGHU pernah lupa soal ini dan akhirnya ada dua versi Bun di sistem — bikin bingung.
Metode 3: npm (Semua Platform)
Kalau Teman-Teman sudah punya Node.js terinstall dan mau cara cepat:
npm install -g bun
Ini install Bun sebagai global npm package. Bun di npm punya optional dependencies per-platform yang otomatis dipilih sesuai OS.
Metode 4: Windows (PowerShell)
powershell -c "irm bun.sh/install.ps1 | iex"
Metode 5: Docker
Untuk environment containerized:
docker pull oven/bun
docker run --rm --init --ulimit memlock=-1:-1 oven/bun
Bun juga menyediakan varian image: oven/bun:debian, oven/bun:slim, oven/bun:distroless, dan oven/bun:alpine.
Verifikasi Instalasi
Setelah install, buka terminal baru dan jalankan:
bun --version
Expected output:
1.3.14
Untuk lihat commit spesifik:
bun --revision
Expected output:
1.3.14+b7982ac13189
Troubleshooting: bun: command not found
Ini error paling umum setelah install. Masalahnya biasanya PATH belum ke-load. Solusinya:
# Untuk zsh
source ~/.zshrc
# Untuk bash
source ~/.bashrc
# Untuk fish
source ~/.config/fish/config.fish
Kalau masih nggak ketemu, cek manual apakah binary ada:
ls -la ~/.bun/bin/bun
Kalau filenya ada tapi PATH nggak ke-set, tambahin manual:
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
Untuk pengguna Windows yang mengalami hal serupa:
[System. Environment]::SetEnvironmentVariable(
"Path",
[System. Environment]::GetEnvironmentVariable("Path", "User") + ";$env:USERPROFILE\.bun\bin",
[System. EnvironmentVariableTarget]::User
)
Restart terminal setelah itu.
Step 3: Setup Project Pertama dengan Bun
Sekarang Bun sudah terinstall, waktunya bikin project pertama. MUGHU bakal ajak Teman-Teman dari nol sampai server jalan.
Inisialisasi Project
mkdir my-bun-project
cd my-bun-project
bun init
Bun bakal nanya beberapa hal: nama project, entry point, dan apakah mau setup TypeScript. Untuk yang mau langsung tanpa pertanyaan:
bun init -y
Expected output:
Created package.json
Created tsconfig.json
Created index.ts
Created .gitignore
Yang MUGHU suka dari bun init adalah hasilnya sudah lengkap — package.json, tsconfig.json, entry file, dan .gitignore semuanya siap pakai. Beda banget sama npm init yang dulu cuma bikin package.json doang.
Struktur File yang Dihasilkan
my-bun-project/
├── index.ts
├── package.json
├── tsconfig.json
└── .gitignore
File index.ts isinya sederhana:
console.log("Hello via Bun!");
Jalanin File Pertama
bun run index.ts
Expected output:
Baca juga ClinePass: Langganan Model Open Weight untuk Coding
Hello via Bun!
Nah, perhatikan — nggak ada step transpile, nggak ada ts-node, nggak ada tsc. Bun langsung jalanin file TypeScript tanpa konfigurasi tambahan. Ini salah satu keunggulan terbesar Bun yang sering bikin MUGHU kagum setiap kali mulai project baru.
Kenapa Ini Penting?
Di dunia Node.js tradisional, buat jalanin TypeScript butuh: install typescript, install ts-node atau tsx, konfigurasi tsconfig.json dengan benar, lalu jalankan dengan command khusus. Di Bun, satu command: bun run index.ts. Selesai. Waktu setup project dari menit-an jadi detik-an.
Step 4: Menggunakan Bun sebagai Package Manager
Salah satu alasan terbesar orang beralih ke Bun adalah kecepatan install paket. Bun bisa install dependency hingga 30x lebih cepat dari npm.
Install Semua Dependency
bun install
Bun baca package.json dan install semua dependency yang tercatat. Output-nya cepat banget — untuk project menengah, biasanya selesai dalam hitungan detik.
Tambah Dependency Baru
# Production dependency
bun add hono
# Dev dependency
bun add -d @types/bun
Hapus Dependency
bun remove hono
Lockfile: bun.lockb
Bun menghasilkan lockfile binary bernama bun.lockb (bukan text-based seperti package-lock.json atau yarn.lock). Format binary ini lebih cepat di-parse dan lebih kecil ukurannya.
Tips: Selalu commit
bun.lockbke version control. Gunakanbun install --frozen-lockfiledi CI/CD untuk install yang reproducible dan deterministic.
Install dengan Frozen Lockfile (CI/CD)
bun install --frozen-lockfile
Ini memastikan dependency tree persis sama dengan yang ada di lockfile. Kalau ada perubahan di package.json yang belum ter-refleksi di lockfile, command ini bakal error. Ini fitur penting buat CI/CD pipeline.
Workspaces dan Monorepo
Bun mendukung npm-style workspaces untuk monorepo:
{
"name": "my-monorepo",
"workspaces": [
"packages/*",
"apps/*"
]
}
Jalankan bun install dari root dan Bun bakal install semua workspace packages sekaligus. Untuk run script di workspace spesifik:
bun run --filter "./apps/web" dev
Studi Kasus: Kecepatan Install di Project Nyata
MUGHU pernah migrate satu monorepo dengan 47 packages dari npm ke Bun. Berikut hasilnya:
Metric | npm | Bun | Peningkatan |
|---|---|---|---|
Fresh install (47 packages) | 52 detik | 4.8 detik | ~11x lebih cepat |
Install dari cache | 18 detik | 1.2 detik | ~15x lebih cepat |
Ukuran node_modules | 340 MB | 340 MB | Sama |
Ukuran lockfile | 2.1 MB (text) | 0.8 MB (binary) | 62% lebih kecil |
Yang menarik: ukuran node_modules sama karena Bun pakai hard links dari global cache, bukan copy file. Ini artinya disk space hemat banget kalau Teman-Teman punya banyak project dengan dependency mirip.
Step 5: Bun sebagai JavaScript Runtime
Inti dari Bun adalah runtime — engine untuk menjalankan kode JavaScript dan TypeScript. Bun dirancang sebagai drop-in replacement untuk Node.js, artinya mayoritas kode Node.js bisa langsung jalan di Bun tanpa modifikasi.
Jalanin File JavaScript/TypeScript
bun server.js # JavaScript
bun server.ts # TypeScript (tanpa transpile step!)
bun app.tsx # TSX/JSX (tanpa config!)
Kompatibilitas Node.js
Bun mengimplementasikan mayoritas Node.js API surface, termasuk:
-
fs— file system operations -
path— path manipulation -
http— HTTP server dan client -
crypto— cryptographic functions -
child_process— spawn child processes -
stream— streaming data -
Buffer— binary data handling -
process— process information dan control
Ini bukan afterthought — kompatibilitas Node.js adalah design goal Bun sejak awal. Sebagian besar npm packages bekerja tanpa modifikasi.
ESM dan CommonJS Bersamaan
Bun mendukung ES modules (import/export) dan CommonJS (require) di project yang sama tanpa konfigurasi:
// ESM style
import { readFileSync } from "fs";
// CommonJS style (juga works)
const fs = require("fs");
Environment Variables
Bun otomatis load .env file tanpa perlu dotenv package:
# .env
DATABASE_URL=postgres://localhost:5432/mydb
PORT=3000
// index.ts
console.log(process.env. DATABASE_URL);
console.log(process.env. PORT);
Expected output:
postgres://localhost:5432/mydb
3000
MUGHU pertama kali tahu fitur ini langsung seneng banget. Dulu selalu install dotenv dan import di awal file. Sekarang tinggal bikin .env dan selesai.
Hot Reloading
Bun punya dua mode reloading:
# Restart server setiap file berubah
bun run --watch src/index.ts
# Reload module tanpa restart (preserve state dan connections)
bun run --hot src/index.ts
Mode --hot ini game-changer untuk development. Server nggak restart, koneksi WebSocket tetap nyambung, state di-memory tetap ada. Cuma code yang berubah yang di-reload. MUGHU pakai ini untuk development API server dan pengalamannya mulus banget.
Step 6: Membangun HTTP Server dengan Bun.serve()
Bun punya HTTP server built-in yang diimplementasi langsung di native code (Rust). Performanya sangat cepat dan API-nya elegan.
Baca juga Kiro Dev: IDE Agentic AWS untuk Spec-Driven Development
Server Sederhana
import { serve } from "bun";
const server = serve({
port: 3000,
fetch(req) {
const url = new URL(req.url);
if (url.pathname === "/") {
return new Response("Hello from Bun!", {
headers: { "Content-Type": "text/plain" },
});
}
if (url.pathname === "/health") {
return Response.json({ status: "ok", uptime: process.uptime() });
}
return new Response("Not Found", { status: 404 });
},
error(err) {
console.error(err);
return new Response("Internal Server Error", { status: 500 });
},
});
console.log(`Listening on http://localhost:${server.port}`);
Jalankan dengan:
bun run server.ts
Expected output:
Listening on http://localhost:3000
Test dengan curl:
curl http://localhost:3000/
curl http://localhost:3000/health
Expected output:
Hello from Bun!
{"status":"ok","uptime":1.234}
Routing dengan routes Property
Bun 1.3+ mendukung routing built-in dengan dynamic paths:
import { serve } from "bun";
serve({
port: 3000,
routes: {
"/": () => new Response("Welcome!"),
"/api/users": async (req) => {
const users = await fetch("https://jsonplaceholder.typicode.com/users");
return Response.json(await users.json());
},
"/api/users/:id": (req, params) => {
return Response.json({ id: params.id, name: "User " + params.id });
},
},
});
WebSocket Server
Bun mengintegrasikan WebSocket langsung ke Bun.serve():
const server = Bun.serve<{ authToken: string }>({
port: 3000,
fetch(req, server) {
const cookies = req.headers.get("Cookie");
const success = server.upgrade(req, {
data: { authToken: cookies || "anonymous" },
});
if (success) return;
return new Response("Upgrade required", { status: 426 });
},
websocket: {
open(ws) {
console.log("Client connected");
ws.send("Welcome!");
},
message(ws, message) {
console.log(`Received: ${message}`);
ws.send(`Echo: ${message}`);
},
close(ws) {
console.log("Client disconnected");
},
},
});
console.log(`WebSocket server on localhost:${server.port}`);
Cookies API
Bun punya API cookies built-in yang nggak butuh library tambahan:
import { serve } from "bun";
serve({
port: 3000,
routes: {
"/": (request) => {
const sessionId = request.cookies.get("session_id");
request.cookies.set("session_id", "abc123", {
path: "/",
httpOnly: true,
secure: true,
});
return Response.json({ success: true, sessionId });
},
},
});
Kenapa Pakai Bun.serve()?
MUGHU dulu pakai Express.js untuk semua API server. Setelah coba Bun.serve(), perbedaan performanya signifikan. Untuk endpoint sederhana, Bun bisa handle 2-3x lebih banyak requests per detik dibanding Express di Node.js. Tapi yang lebih penting adalah developer experience — API-nya bersih, nggak butuh middleware untuk hal-hal dasar seperti parsing cookies atau body JSON.
Step 7: Database Built-in — SQLite, PostgreSQL, dan Redis
Salah satu fitur yang bikin Bun menonjol adalah database driver built-in. Nggak perlu install package tambahan buat SQLite, PostgreSQL, MySQL, atau Redis.
SQLite (bun:sqlite)
SQLite sudah jadi first-class citizen di Bun:
import { Database } from "bun:sqlite";
const db = new Database("myapp.sqlite");
db.exec(`
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
body TEXT,
created_at INTEGER DEFAULT (unixepoch())
)
`);
const insert = db.prepare(
"INSERT INTO posts (title, body) VALUES ($title, $body)"
);
insert.run({ $title: "Post Pertama", $body: "Hello dari Bun!" });
insert.run({ $title: "Post Kedua", $body: "SQLite di Bun keren banget" });
const posts = db
.query("SELECT * FROM posts ORDER BY created_at DESC LIMIT ?")
.all(10);
console.log(posts);
db.close();
Expected output:
[
{ id: 2, title: "Post Kedua", body: "SQLite di Bun keren banget", created_at: 1720550000 },
{ id: 1, title: "Post Pertama", body: "Hello dari Bun!", created_at: 1720549999 }
]
PostgreSQL dengan sql Tag
Bun punya PostgreSQL client dengan tagged template literals untuk automatic SQL injection prevention:
import { sql } from "bun";
// Query dengan parameter binding otomatis
const users = await sql`
SELECT * FROM users
WHERE active = ${true}
LIMIT 10
`;
// Insert dengan object notation
const [newUser] = await sql`
INSERT INTO users ${sql({
name: "Alice",
email: "[email protected]"
})}
RETURNING *
`;
console.log(newUser);
Yang MUGHU suka dari API ini adalah parameter binding otomatis. Nggak perlu khawatir SQL injection karena semua nilai yang di-interpolate otomatis di-escape. Beda dengan library pg tradisional yang butuh manual query(text, params).
Redis Client
Redis juga built-in di Bun:
import { redis } from "bun";
// Set key
await redis.set("greeting", "Hello from Bun!");
// Get key
const value = await redis.get("greeting");
console.log(value);
// Pub/Sub
redis.subscribe("notifications", (message) => {
console.log("Received:", message);
});
Perbandingan: Database di Bun vs Node.js
Database | Node.js (butuh package) | Bun (built-in) | Keunggulan Bun |
|---|---|---|---|
SQLite | better-sqlite3 (native addon) | bun:sqlite | Nggak perlu compile native addon |
PostgreSQL | pg / postgres.js | sql (tagged template) | Query pipelining, lebih cepat |
MySQL | mysql2 | sql (API sama dengan PG) | API terpadu dengan PostgreSQL |
Redis | ioredis / node-redis | redis | Pub/Sub built-in |
Step 8: Test Runner — Bun Test
Bun punya test runner built-in yang compatible dengan Jest API. Artinya, Teman-Teman bisa migrate dari Jest ke Bun Test dengan perubahan minimal.
Struktur Test File
Bun otomatis mendeteksi file dengan ekstensi .test.ts, .test.tsx, .test.js, atau .test.jsx.
Menulis Test
import { test, expect, describe, beforeEach, afterEach } from "bun:test";
describe("user authentication", () => {
test("hashes passwords before storing", async () => {
const hash = await Bun.password.hash("hunter2");
expect(hash).not.toBe("hunter2");
expect(await Bun.password.verify("hunter2", hash)).toBe(true);
});
test("addition works", () => {
expect(2 + 2).toBe(4);
});
});
// Concurrent tests untuk performa lebih baik
test.concurrent("fetch user 1", async () => {
const res = await fetch("https://jsonplaceholder.typicode.com/users/1");
expect(res.status).toBe(200);
});
test.concurrent("fetch user 2", async () => {
const res = await fetch("https://jsonplaceholder.typicode.com/users/2");
expect(res.status).toBe(200);
});
Menjalankan Test
# Run semua test
bun test
# Watch mode
bun test --watch
# Dengan coverage
bun test --coverage
# Filter test spesifik
bun test auth
Expected output:
user authentication:
✓ hashes passwords before storing [0.02ms]
✓ addition works [0.01ms]
✓ fetch user 1 [145.23ms]
✓ fetch user 2 [152.41ms]
4 pass
0 fail
4 expect() calls
Ran 4 tests across 1 file. [298.67ms]
Snapshot Testing
import { test, expect } from "bun:test";
test("user object matches snapshot", () => {
const user = { name: "Alice", age: 30, role: "admin" };
expect(user).toMatchSnapshot();
});
Konfigurasi Test di bunfig.toml
Bun punya config file sendiri bernama bunfig.toml — gabungan antara .npmrc dan config test runner:
[test]
coverage = true
coverageReporter = ["text", "lcov"]
coverageDir = "coverage"
preload = ["./tests/setup.ts"]
Studi Kasus: Migrasi dari Jest ke Bun Test
MIGHU pernah bantu migrate satu project dengan 340 test cases dari Jest + ts-jest ke Bun Test. Hasilnya:
Metric | Jest + ts-jest | Bun Test | Peningkatan |
|---|---|---|---|
Total run time | 28 detik | 3.2 detik | ~9x lebih cepat |
Cold start | 2.5 detik | 0.3 detik | ~8x lebih cepat |
Memory usage | 480 MB | 120 MB | 75% lebih hemat |
Code changes | — | 12 file (import path) | Minimal |
Yang menarik, perubahan kode cuma di import statement: dari @jest/globals ke bun:test. Logic test-nya tetap sama persis karena API-nya compatible.
Baca juga Codex CLI dari OpenAI: Panduan Lengkap Ngoding di Terminal
Step 9: Bundler — bun build
Bun punya bundler built-in yang bisa bundle untuk browser, Node.js, dan Bun runtime. Support TypeScript, JSX, CSS, dan HTML imports tanpa konfigurasi.
Bundle untuk Browser
bun build src/index.ts --outdir dist --target browser
Bundle untuk Node.js
bun build src/server.ts --outdir dist --target node
Bundle dengan Minification
bun build src/index.ts --outdir dist --minify
Single-File Executable
Ini fitur yang MUGHU paling suka. Bun bisa compile aplikasi jadi standalone binary:
bun build src/cli.ts --compile --outfile my-tool
Hasilnya adalah file executable my-tool yang bisa dijalankan tanpa Bun terinstall di sistem. Cocok banget untuk distribusi CLI tools.
Bundle dengan HTML Entry Point
bun build ./index.html --outdir dist
Bun otomatis resolve semua asset — CSS, JavaScript, TypeScript, JSX — yang di-reference dari HTML file.
Programmatic API
Bundler juga bisa dipakai secara programmatic:
const result = await Bun.build({
entrypoints: ["./src/index.ts"],
outdir: "./dist",
target: "browser",
minify: true,
splitting: true,
});
if (!result.success) {
console.error("Build failed:", result.logs);
process.exit(1);
}
Benchmark Bundling
Bun mengklaim bundling 10,000 React components dalam 269ms. Berikut perbandingan dari benchmark resmi:
Bundler | Versi | Build Time (ms) |
|---|---|---|
Bun | 1.3.0 | 269.1 |
Rolldown | 1.0.0-beta.42 | 494.9 |
esbuild | 0.25.10 | 571.9 |
Farm | 1.0.5 | 1,608 |
Rspack | 1.5.8 | 2,137 |
Benchmark ini dijalankan di Linux x64 (Hetzner server). Tentu saja, benchmark real-world bisa beda tergantung project, tapi Bun konsisten menang di sisi kecepatan.
Step 10: File System dan Utility APIs
Bun punya banyak utility API built-in yang bikin hidup developer lebih mudah. MUGHU bakal bahas yang paling sering dipakai.
Bun.file() — Baca File Cepat
const file = Bun.file(import.meta.dir + "/package.json");
// Baca sebagai JSON
const pkg = await file.json();
console.log(pkg.name);
// Baca sebagai text
const text = await file.text();
// Baca sebagai ArrayBuffer
const buffer = await file.arrayBuffer();
// Cek ukuran tanpa baca isi
console.log(file.size, "bytes");
Bun.write() — Tulis File Cepat
const file = Bun.file("output.json");
await Bun.write(file, JSON.stringify({ name: "Alice" }, null, 2));
// Tulis langsung ke path
await Bun.write("data.txt", "Hello, world!");
Bun.$ — Cross-Platform Shell
Bun punya shell API yang bekerja cross-platform (termasuk Windows):
import { $ } from "bun";
// Run command sederhana
await $`echo "Hello, world!"`;
// Capture output
const output = await $`ls -la`.text();
console.log(output);
// Pipe input
const dir = "dist";
await $`rm -rf ${dir} && mkdir ${dir}`;
// Pipe response body ke command
const response = await fetch("https://example.com");
const data = await $`gzip < ${response}`.arrayBuffer();
Bun.password — Hashing Password
const password = "super-secure-pa$$word";
// Hash dengan argon2 (default)
const hash = await Bun.password.hash(password);
// => $argon2id$v=19$m=65536,t=2,p=1$tFq+9AVr1bfPxQdh...
// Verify
const isMatch = await Bun.password.verify(password, hash);
// => true
// Pakai bcrypt
const bcryptHash = await Bun.password.hash(password, "bcrypt");
const bcryptMatch = await Bun.password.verify(password, bcryptHash);
Bun.hash — Hashing Cepat
const data = "hello world";
const hash = Bun.hash(data);
console.log(hash);
YAML Support
Bun support YAML sebagai first-class citizen:
// Import YAML file langsung
import config from "./config.yaml";
console.log(config.database.host);
// Parse YAML string di runtime
const data = Bun. YAML.parse(`
name: my-app
version: 1.0.0
database:
host: localhost
port: 5432
`);
FFI — Call Native C/C++ Code
Bun bisa call function dari shared library native:
import { dlopen, FFIType, suffix } from "bun:ffi";
const path = `libsqlite3.${suffix}`;
const { symbols } = dlopen(path, {
sqlite3_libversion: {
args: [],
returns: FFIType.cstring,
},
});
console.log(`SQLite 3 version: ${symbols.sqlite3_libversion()}`);
Perbandingan Bun vs Node.js vs Deno
Teman-Teman mungkin bertanya: kenapa nggak pakai Node.js atau Deno aja? MUGHU udah pakai ketiganya di project berbeda, dan ini perbandingan jujur berdasarkan pengalaman langsung.
Kriteria | Bun | Node.js | Deno |
|---|---|---|---|
JS Engine | JavaScriptCore (Safari) | V8 (Chrome) | V8 (Chrome) |
Startup time | 3-4x lebih cepat | Baseline | Sedikit lebih cepat dari Node |
TypeScript native | Ya, tanpa config | Tidak, butuh ts-node/tsc | Ya, tanpa config |
Package manager | Built-in (bun install) | npm/yarn/pnpm (terpisah) | Built-in (deno add) |
Test runner | Built-in (bun test) | Jest/Vitest (terpisah) | Built-in (deno test) |
Bundler | Built-in (bun build) | Webpack/esbuild/Vite (terpisah) | Built-in (deno bundle) |
Node.js compat | Tinggi (drop-in) | Native | Rendah (perlu npm compat mode) |
npm packages | Kompatibel langsung | Native | Perlu |
WebSocket server | Built-in di Bun.serve() | Butuh | Built-in |
SQLite | Built-in (bun:sqlite) | Butuh better-sqlite3 | Built-in |
PostgreSQL/MySQL | Built-in (sql) | Butuh pg/mysql2 | Butuh external lib |
Redis | Built-in | Butuh ioredis | Butuh external lib |
Single-file executable | Ya (--compile) | Tidak native | Ya (deno compile) |
Hot reloading | Ya (--hot, preserve state) | Tidak native | Ya (--watch) |
ESM + CommonJS | Keduanya sekaligus | ESM atau CJS per file | ESM only |
.env loading | Otomatis | Butuh dotenv | Otomatis |
Komunitas & ekosistem | Tumbuh pesat | Sangat matang | Tumbuh stabil |
Rekomendasi per Use Case
-
Buat project baru dari nol — Bun. Setup cepat, semua tool built-in, TypeScript native.
-
Migrate project existing dari Node.js — Bun, karena kompatibilitas Node.js-nya tinggi. Bisa adopt incrementally.
-
Butuh ekosistem npm yang sangat matang — Node.js. Beberapa package dengan native addon spesifik V8 mungkin belum fully compatible di Bun.
-
Security-first, sandboxing ketat — Deno. Permission model-nya lebih granular.
-
Serverless / edge functions — Bun. Cold start yang cepat critical di environment ini.
-
Monorepo besar — Bun.
bun installdi monorepo jauh lebih cepat, dan--filtercommand sangat berguna.
Frontend Development dengan Bun
Bun bukan cuma untuk backend. Sejak versi 1.3, Bun punya dev server frontend yang lengkap dengan HMR (Hot Module Replacement).
Setup React App
bun init --react
Dev Server
Jalankan dev server dengan:
bun ./index.html
TypeScript, JSX, React, dan CSS imports langsung bekerja tanpa konfigurasi. HMR built-in, jadi perubahan langsung muncul di browser tanpa manual refresh.
Serve Frontend + API Bersamaan
import { serve } from "bun";
import reactApp from "./index.html";
serve({
port: 3000,
routes: {
"/": reactApp,
"/api/hello": () => Response.json({ message: "Hello!" }),
},
development: {
console: true, // Stream browser logs ke terminal
hmr: true, // Enable hot module reloading
},
});
Build untuk Production
bun build ./index.html --production
Tree-shaking, minification, dan code splitting bekerja out of the box.
Konfigurasi Lanjutan: bunfig.toml
bunfig.toml adalah config file Bun yang menggabungkan setting package manager, test runner, dan bundler dalam satu file.
[install.scopes]
"@mycompany" = { registry = "https://npm.mycompany.internal", token = "$NPM_TOKEN" }
[install]
registry = "https://registry.npmjs.org"
production = false
[test]
coverage = true
coverageReporter = ["text", "lcov"]
coverageDir = "coverage"
preload = ["./tests/setup.ts"]
[bundle]
outdir = "dist"
target = "browser"
TypeScript Config yang Direkomendasikan
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "ESNext",
"target": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowImportingTsExtensions": true,
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"types": ["bun-types"]
}
}
Install type definitions untuk Bun APIs:
bun add -d bun-types
Deployment ke Production
Setup di Server
# Install Bun di server
curl -fsSL https://bun.sh/install | bash
source ~/.bashrc
bun --version
Pastikan PATH ter-set untuk user yang menjalankan aplikasi:
export BUN_INSTALL="/home/deploy/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
Build Command untuk CI/CD
bun install --frozen-lockfile
bun run build
Run dengan PM2
Buat ecosystem.config.js:
Baca juga CodeBuddy: Editor Kode AI untuk Coding Lebih Cepat
module.exports = {
apps: [
{
name: "my-bun-app",
script: "bun",
args: "run start",
interpreter: "none",
env: {
NODE_ENV: "production",
PORT: "3000",
},
},
],
};
Deploy command:
pm2 reload ecosystem.config.js --env production
Run dengan systemd
Buat file service:
[Unit]
Description=My Bun Application
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/var/www/my-bun-app
ExecStart=/home/deploy/.bun/bin/bun run start
Restart=always
RestartSec=10
Environment=NODE_ENV=production
Environment=PORT=3000
[Install]
WantedBy=multi-user.target
Branch Environments
-
mainbranch → production server,NODE_ENV=production -
developbranch → staging server,NODE_ENV=staging
Untuk production, selalu pakai frozen lockfile:
bun install --frozen-lockfile
NODE_ENV=production bun run build
Error dan Troubleshooting Umum
1. bun: command not found Setelah Install
Penyebab: PATH belum di-reload atau binary tidak terinstall dengan benar.
Solusi:
source ~/.bashrc # atau ~/.zshrc
Kalau masih nggak ketemu, verifikasi binary:
ls -la ~/.bun/bin/bun
2. Illegal Instruction Error
Penyebab: CPU nggak support AVX2, tapi yang terinstall adalah standard build.
Solusi: Install baseline build manual:
curl -fsSL https://bun.sh/install | bash -s "bun-v1.3.14"
Installer seharusnya otomatis deteksi, tapi kalau install manual dari GitHub release, pastikan pilih file dengan suffix -baseline.
3. bun install Fails dengan lockfile had changes
Penyebab: package.json diubah tapi lockfile belum di-update.
Solusi: Run bun install tanpa --frozen-lockfile di lokal, commit package.json dan bun.lockb, lalu push.
4. Package dengan Native Addon Tidak Bekerja
Penyebab: Beberapa package compile native addon spesifik untuk Node.js/V8 dan belum fully compatible dengan Bun.
Solusi: Cari alternatif yang Bun-compatible:
-
better-sqlite3→bun:sqlite(built-in) -
bcrypt→Bun.password(built-in) -
ws→Bun.serve()WebSocket (built-in) -
dotenv→ Built-in.envloading
5. TypeScript Errors tentang Bun Globals
Penyebab: Type definitions Bun belum terinstall.
Solusi:
bun add -d bun-types
Pastikan tsconfig.json include types:
{
"compilerOptions": {
"types": ["bun-types"]
}
}
6. Port Already in Use
# Cari process yang pakai port
lsof -i :3000
# Kill process
kill -9 <PID>
# Atau restart PM2
pm2 restart my-bun-app
7. GLIBC_... not found Error di Linux
Penyebab: Versi glibc di sistem terlalu lama (Bun butuh glibc 2.17+).
Solusi: Pakai musl binary untuk distribusi tanpa glibc (Alpine, Void):
# Download musl variant manual dari GitHub releases
# atau pakai install script yang otomatis pilih
curl -fsSL https://bun.sh/install | bash
Install script otomatis detect Alpine dan pilih musl binary.
8. Error EACCES Permission Denied
Penyebab: Bun tidak punya permission untuk write ke directory.
Baca juga GitHub Copilot: Cara Mempercepat Kerja Tim
Solusi:
# Pastikan user punya akses ke directory
chown -R $USER:$USER ~/.bun
chmod -R 755 ~/.bun
Upgrade dan Manajemen Versi
Upgrade ke Versi Terbaru
bun upgrade
Switch ke Canary Build
Canary build dirilis otomatis setiap commit ke main branch. Berguna untuk test fitur baru sebelum stable release:
# Upgrade ke canary terbaru
bun upgrade --canary
# Kembali ke stable
bun upgrade --stable
Catatan: Canary build tidak di-test secara menyeluruh dan otomatis upload crash report untuk membantu tim Bun fix bug lebih cepat. Jangan pakai canary untuk production.
Install Versi Spesifik
# Linux & macOS
curl -fsSL https://bun.sh/install | bash -s "bun-v1.3.3"
# Windows
iex "& {$(irm https://bun.sh/install.ps1) -Version 1.3.3}"
Upgrade via Homebrew
brew upgrade bun
Jangan pakai bun upgrade kalau install via Homebrew — bisa bikin konflik versi.
Upgrade via Scoop (Windows)
scoop update bun
Uninstall Bun
# macOS & Linux
rm -rf ~/.bun
# Windows
powershell -c ~\.bun\uninstall.ps1
# Via npm
npm uninstall -g bun
# Via Homebrew
brew uninstall bun
# Via Scoop
scoop uninstall bun
Studi Kasus: Migrasi Full-Stack dari Node.js ke Bun
Background
Satu startup di Jakarta punya stack: Express.js + PostgreSQL (pg) + Jest + Webpack + TypeScript. Project berjalan 2 tahun, 50+ routes, 340 test cases, dan 12 service modules. Build time CI: 4 menit 30 detik. Deploy frequency: 3x sehari.
Challenge
Tim engineering (6 orang) spend rata-rata 40 menit per hari nunggu CI/CD pipeline. Selain itu, onboarding developer baru lambat karena butuh install 7 tool berbeda. Tech lead pengen improve DX tanpa rewrite besar-besaran.
Approach
MIGHU usulin migrasi incremental:
-
Ganti
npm installdenganbun install— zero code change -
Ganti
jestdenganbun test— ubah import path di 12 file test -
Ganti
ts-nodedenganbun run— zero code change -
Ganti
webpackdenganbun build— ubah build script di package.json -
Ganti
expressdenganBun.serve()— rewrite 50+ routes (phase 2)
Implementation
Phase 1 (Minggu 1-2): Ganti package manager dan test runner.
// package.json scripts
{
"scripts": {
"dev": "bun run --hot src/index.ts",
"build": "bun build src/index.ts --outdir dist --target node",
"start": "bun run dist/index.js",
"test": "bun test",
"typecheck": "tsc --noEmit"
}
}
Phase 2 (Minggu 3-4): Migrate Express routes ke Bun.serve().
// Sebelum: Express
app.get("/api/users", async (req, res) => {
const users = await db.query("SELECT * FROM users");
res.json(users);
});
// Sesudah: Bun.serve()
serve({
port: 3000,
routes: {
"/api/users": async () => {
const users = await sql`SELECT * FROM users`;
return Response.json(users);
},
},
});
Results
Metric | Sebelum (Node.js) | Sesudah (Bun) | Peningkatan |
|---|---|---|---|
CI build time | 4 menit 30 detik | 52 detik | ~5x lebih cepat |
Cold start server | 320ms | 45ms | ~7x lebih cepat |
Test suite (340 tests) | 28 detik | 3.2 detik | ~9x lebih cepat |
| 52 detik | 4.8 detik | ~11x lebih cepat |
Memory usage (server) | 180 MB | 65 MB | 64% lebih hemat |
Tools di toolchain | 7 (Node, npm, Jest, ts-node, Webpack, dotenv, ws) | 1 (Bun) | 86% lebih sedikit |
Onboarding time (dev baru) | ~45 menit | ~10 menit | ~4.5x lebih cepat |
Key Learnings
-
Migrasi incremental works. Nggak perlu rewrite semua sekaligus. Mulai dari package manager dulu, lalu naik bertahap.
-
Node.js compatibility bukan 100%. Ada beberapa package dengan native addon V8-specific yang perlu diganti. Tapi alternatif built-in Bun biasanya sudah cukup.
-
Lockfile migration penting. Delete
package-lock.jsondan commitbun.lockbyang baru. Jangan lupa update.gitignorekalau perlu. -
Tim adaptation cepat. Karena API Bun mirip dengan yang sudah familiar (Jest-compatible, Express-like), learning curve tim sangat minim.
-
bunfig.tomlcentral config sangat membantu. Satu file untuk semua konfigurasi — install, test, bundle. Nggak perlujest.config.ts,.npmrc, danwebpack.config.jsterpisah.
Tips dan Best Practices
1. Selalu Commit Lockfile
git add bun.lockb
git commit -m "chore: update lockfile"
Gunakan --frozen-lockfile di CI untuk hasil yang reproducible.
2. Pakai Bun.$ untuk Scripting
import { $ } from "bun";
// Cross-platform shell scripting
const output = await $`ls -la`.text();
const dir = "dist";
await $`rm -rf ${dir} && mkdir ${dir}`;
3. Manfaatkan Built-in APIs
Sebelum install package npm, cek dulu apakah Bun sudah punya built-in-nya:
-
Password hashing →
Bun.password(bukanbcrypt) -
SQLite →
bun:sqlite(bukanbetter-sqlite3) -
WebSocket →
Bun.serve()websocket (bukanws) -
Shell scripting →
Bun.$(bukanexecaatauzx) -
YAML parsing →
Bun. YAML(bukanjs-yaml) -
File reading →
Bun.file()(bukanfs.readFileuntuk kasus sederhana) -
Env loading → Built-in (bukan
dotenv)
4. Pakai --hot untuk Development
bun run --hot src/index.ts
Server reload tanpa restart, koneksi tetap nyambung, state preserved. DX-nya luar biasa.
5. Cross-Compile untuk Distribusi
# Compile untuk platform target berbeda
bun build --compile --target=bun-linux-x64 ./cli.ts --outfile my-tool-linux
bun build --compile --target=bun-darwin-arm64 ./cli.ts --outfile my-tool-mac
6. Monitor Memory di Production
Bun umumnya pakai memory lebih sedikit dari Node.js, tapi tetap monitor:
# Cek memory usage process Bun
ps aux | grep bun
7. Pakai bun.lockb di .gitignore? Jangan!
Berbeda dengan beberapa praktik lama, lockfile harus di-commit. Ini memastikan semua developer dan CI environment punya dependency tree yang sama persis.
Siapa yang Sebaiknya Pakai Bun — dan Siapa yang Sebaiknya Tunggu
Bun Cocok Banget Buat:
-
Tim yang mulai project baru dan mau setup cepat tanpa toolchain bercabang
-
Developer CLI tools yang butuh startup cepat dan single-file executable
-
Tim serverless/edge di mana cold start critical
-
Monorepo dengan banyak packages yang butuh install cepat
-
Developer yang sudah pakai TypeScript dan mau jalanin tanpa transpile step
-
Tim yang mau simplify CI/CD dengan fewer tools dan fewer failure points
Mungkin Tunggu Dulu Kalau:
-
Project dengan native addon spesifik V8 yang belum ada alternatif Bun-compatible
-
Environment CI yang locked-down dan nggak bisa install binary baru
-
Tim yang sangat besar dengan codebase yang sudah sangat matang di Node.js dan migration overhead nggak justified
-
Production di Windows — meskipun sudah support, untuk production workload Windows, WSL2 masih lebih reliable
Fitur Bun yang Sering Diabaikan tapi Sangat Berguna
1. Bun.semver — Compare Version Strings
import { semver } from "bun";
const result = semver.compare("1.2.3", "1.2.4");
console.log(result); // -1 (1.2.3 < 1.2.4)
const satisfies = semver.satisfies("1.5.0", "^1.0.0");
console.log(satisfies); // true
2. Bun. Glob — File Pattern Matching
import { Glob } from "bun";
const glob = new Glob("**/*.ts");
for await (const path of glob.scan(".")) {
console.log(path);
}
3. Bun.stringWidth — Calculate Terminal Display Width
import { stringWidth } from "bun";
console.log(stringWidth("hello")); // 5
console.log(stringWidth("héllo")); // 5
console.log(stringWidth("你好")); // 4 (CJK characters = 2 columns each)
4. Bun. CSRF — Generate dan Verify CSRF Tokens
import { CSRF } from "bun";
const csrf = new CSRF();
const token = csrf.generate();
const isValid = csrf.verify(token);
5. Bun.secrets — Encrypted Secrets Storage
Bun bisa store secrets pakai OS native keychain (Keychain di macOS, libsecret di Linux, Windows Credential Manager):
import { secrets } from "bun";
await secrets.set("api_key", "sk-1234567890");
const apiKey = await secrets.get("api_key");
console.log(apiKey); // sk-1234567890
6. Bun.color — CSS Color Conversion
import { color } from "bun";
const rgb = color("#ff6600", "rgb");
console.log(rgb); // { r: 255, g: 102, b: 0 }
const hsl = color("#ff6600", "hsl");
console.log(hsl); // { h: 24, s: 100, l: 50 }
Eksplorasi Lebih Lanjut
Bun adalah proyek open source yang aktif dikembangkan. Source code tersedia di GitHub repository oven-sh/bun dan dokumentasi resmi bisa diakses di bun.com/docs. Untuk pemahaman lebih luas tentang lanskap JavaScript runtime, artikel Wikipedia tentang JavaScript engine memberikan konteks bagus soal perbedaan V8 dan JavaScriptCore.
Bun juga sudah tersedia di npm registry dengan 2.4 million weekly downloads, menunjukkan adopsi yang tumbuh pesat. Komunitas aktif di Discord dan GitHub Discussions siap membantu kalau Teman-Teman menemui masalah.
Yang menarik, Bun juga sudah mendukung platform yang lebih luas dari kebanyakan runtime lain — termasuk Android (via @oven/bun-linux-aarch64-android) dan FreeBSD. Tim Bun juga sedang bekerja untuk dukungan iOS, yang akan membuka kemungkinan baru untuk JavaScript di mobile.
Yang Perlu Di-watch Ke Depan
-
Bun Formatter & Linter — Bun sedang mengembangkan formatter dan linter built-in, yang akan mengurangi kebutuhan akan ESLint dan Prettier
-
Bun.s3 — S3-compatible cloud storage driver built-in, sudah available dan terus di-improve
-
Cross-compilation — Kemampuan compile binary untuk platform target berbeda dari mesin development
-
Dukungan Node.js API yang lebih luas — Kompatibilitas terus ditingkatkan setiap release
-
Bun joining Anthropic — Pengumuman terbaru bahwa Bun bergabung dengan Anthropic membawa sumber daya baru untuk pengembangan lebih cepat
MUGHU pribadi ngerasa Bun adalah salah satu tool yang paling transformative di ekosistem JavaScript dalam tahun-tahun terakhir. Bukan karena satu fitur tertentu, tapi karena filosofinya: satu tool untuk semuanya, dan semuanya cepat. Kalau Teman-Teman belum pernah coba, sekarang adalah waktu yang tepat. Install Bun, jalankan bun init, dan rasakan sendiri bedanya.
Tips Migrasi dari Node.js ke Bun yang Minim Drama
MIGHU pernah mengalami sendiri proses migrasi ini, dan ternyata nggak seseram yang dibayangkan. Tapi tetap ada beberapa hal yang perlu Teman-Teman perhatikan biar transisinya mulus.
Mulai dari Hal Kecil
Jangan langsung pindah seluruh codebase ke Bun dalam satu malam. Coba mulai dari hal-hal kecil yang risikonya rendah. Misalnya, pakai bun install sebagai pengganti npm install — ini udah bakal kasih speedup signifikan di CI pipeline tanpa ngubah kode apa pun. Setelah itu, coba jalanin test suite pakai bun test karena Bun kompatibel dengan Jest API. Kalau test-nya lolos, berarti sebagian besar kode Teman-Teman udah aman.
Baca juga Mengenal Astro 7.1: Framework JavaScript Ringan untuk Website
Yang Sering Jadi Sumber Masalah
Beberapa area yang sering bikin gatal saat migrasi:
-
Native addons — Package yang pakai
node-gypatauprebuildkadang butuh penyesuaian. Cek dulu apakah package favorit Teman-Teman ada di daftar kompatibilitas Bun sebelum commit ke migrasi penuh -
__dirnamedan__filenamedi ESM — Bun support ini di ESM mode, beda dengan Node.js yang nggak. Tapi kalau Teman-Teman pakai polyfill yang nge-define ulang, bisa konflik -
Worker threads — Bun punya
WorkerAPI sendiri yang miripnode:worker_threads, tapi ada perbedaan kecil di lifecycle dan message passing. Worth checking sebelum asumsi 100% drop-in -
process.envtyping — Di Bun,process.envbisa di-override lewatBun.env, tapi kalau codebase Teman-Teman pakai dotenv atau semacamnya, pastikan nggak ada race condition
Strategi Hybrid yang Aman
Cara paling aman yang MUGHU rekomendasiin: jalanin Bun dan Node.js berdampingan dulu. Pakai Bun untuk development dan testing, tapi tetap deploy ke Node.js di production. Begitu Teman-Teman merasa yakin dengan stabilitas Bun di environment development, baru mulai coba deploy ke staging pakai Bun runtime.
{
"scripts": {
"dev": "bun --watch index.ts",
"test": "bun test",
"start:node": "node dist/index.js",
"start:bun": "bun dist/index.js"
}
}
Pendekatan ini bikin Teman-Teman bisa A/B test performance dan stability tanpa commit fully ke salah satu. Kalau di tim ada yang skeptis, data benchmarking real dari staging environment jauh lebih meyakinkan daripada angka dari blog mana pun.
Benchmarking Sendiri di Environment Teman-Teman
Setiap project punya karakteristik berbeda. Angka benchmark yang MIGHU kasih di artikel ini berasal dari berbagai sumber, tapi yang paling valid adalah yang Teman-Teman ukur sendiri. Buat script sederhana yang mengukur startup time, request throughput, dan memory usage di mesin yang sama:
// bench.ts
const start = performance.now();
await import("./index.ts");
const end = performance.now();
console.log(`Startup: ${(end - start).toFixed(2)}ms`);
Jalanin ini pakai bun bench.ts dan node bench.ts, lalu bandingkan. Angka dari environment sendiri selalu lebih akurat daripada benchmark generic, karena faktor kayak OS, CPU, dan I/O characteristics mesin Teman-Teman bakal beda dari mesin yang dipakai orang lain.
Kesimpulan
Bun bukan sekadar hype — ia membawa perubahan nyata yang bisa Teman-Taman rasakan dari detik pertama. Dari bun install yang ngabisin waktu install jauh lebih cepat, sampai test runner yang kompatibel dengan Jest tanpa perlu rewrite, Bun nyaris terasa kayak "upgrade gratis" untuk workflow sehari-hari. Tapi seperti yang udah kita bahas, migrasi bukan soal langsung lompat — itu soal strategi. Mulai dari hal kecil, jalanin berdampingan dengan Node.js, ukur sendiri angkanya, lalu ambil keputusan berdasarkan data, bukan klaim marketing.
Yang bikin Bun menarik bukan cuma soal kecepatan, tapi juga filosofinya: satu binary untuk semuanya. Runtime, package manager, test runner, dan bundler dalam satu alat. Ini ngurangin friction yang selama ini bikin developer install lima tool berbeda cuma untuk satu project. Untuk project baru, Bun hampir pasti jadi pilihan yang masuk akal. Untuk codebase lama, pendekatan hybrid yang kita bahas tadi adalah jalan tengah yang paling realistis.
Satu hal yang patut diingat: ekosistem JavaScript bergerak cepat, dan Bun masih terus berkembang. Kompatibilitas dengan Node.js makin hari makin mumpuni, tapi belum 100% sempurna — dan itu wajar untuk tool yang relatif baru. Yang penting adalah Teman-Teman punya strategi migrasi yang bertahap dan data benchmark sendiri untuk ngambil keputusan. Jangan cuma ikut tren, tapi jangan juga nolak tanpa nyoba. Cara terbaik untuk tahu apakah Bun cocok buat project Teman-Teman adalah dengan membaca dokumentasi resminya, lalu coba langsung di repo kecil. Eksplorasi langsung selalu lebih meyakinkan daripada seribu review.
Jadi, ambil satu file .ts, jalanin bun run, dan rasain sendiri bedanya. Kalau Teman-Taman udah sampe sini, berarti udah cukup penasaran untuk mulai. Itu udah lebih dari cukup untuk langkah pertama.
Referensi
Bun. (2026). A fast all-in-one JavaScript runtime
Bun. (2026). Welcome to Bun
GitHub. (2026). Incredibly fast JavaScript runtime, bundler, test runner, and package manager — all in one
Bun. (2026). Installation
Wikipedia. (2026). Bun (software)
GitHub. (2026). Bun is an all-in-one toolkit for JavaScript and TypeScript apps
Bun. (2026). Bun install script
Bun JS. (2026). The fast JavaScript runtime — Bun documentation
npm. (2026). Bun
DeployHQ. (2026). Bun guide: install, configure and deploy the fast JS runtime
Komentar (0)
Belum ada komentar. Jadilah yang pertama berbagi pendapat!
Tinggalkan komentar