DevToolBoxFREE
BlogAdvertise

Bun Package Manager: Runtime JavaScript Tercepat di 2026

12 menitoleh DevToolBox

Bun adalah runtime JavaScript all-in-one yang ditulis dalam Zig. Package manager-nya menginstal dependensi hingga 23x lebih cepat dari npm.

Instalasi dan Perintah Dasar

Bun terinstal dalam hitungan detik dan bekerja sebagai pengganti langsung perintah npm.

# Install Bun
curl -fsSL https://bun.sh/install | bash

# Verify installation
bun --version  # 1.x.x

# Install dependencies (reads package.json)
bun install

# Install a specific package
bun add express
bun add -d typescript @types/node   # dev dependency
bun add -g bunx                     # global install

# Remove a package
bun remove lodash

# Update all packages
bun update

# Run a script from package.json
bun run build
bun run dev

# Execute a TypeScript file directly (no transpile step)
bun run index.ts

Workspaces (Dukungan Monorepo)

Bun mendukung workspaces gaya npm untuk monorepo.

// package.json — monorepo root
{
  "name": "my-monorepo",
  "workspaces": [
    "packages/*",
    "apps/*"
  ],
  "scripts": {
    "dev": "bun run --filter '*' dev",
    "build": "bun run --filter '*' build",
    "test": "bun test"
  }
}

// Run a script only in specific workspace
bun run --filter @myapp/web dev

// Install a dependency in a specific workspace
bun add react --cwd apps/web

// Link a local workspace package
// (Bun automatically resolves workspace: protocol)

Lockfile Biner (bun.lockb)

Bun menggunakan lockfile biner alih-alih JSON.

# bun.lockb is a binary lockfile (much faster to parse than JSON)
# To view lockfile contents in human-readable form:
bun bun.lockb  # prints as yarn.lock format

# To regenerate lockfile:
bun install --frozen-lockfile  # CI: fails if lockfile is stale

# Migrating from npm/yarn/pnpm:
# Bun reads package-lock.json, yarn.lock, pnpm-lock.yaml
# to preserve existing versions on first install

# .gitignore for Bun:
# node_modules/
# *.lock  (optional — commit bun.lockb for reproducible installs)

Benchmark Performa

Package manager Bun jauh lebih cepat dari semua alternatif.

# Benchmark: installing react + 1400 packages
# (warm cache, MacBook M2 Pro)

npm install:   28.3s
yarn install:  14.1s
pnpm install:   9.4s
bun install:    1.2s   # 23x faster than npm!

# Cold cache (first install, no node_modules)
npm install:   38.7s
bun install:    4.1s   # 9x faster

# bun install uses:
# - Symlink-based node_modules (like pnpm)
# - Binary lockfile (bun.lockb) for fast parsing
# - Parallel downloads
# - Global package cache (~/.bun/install/cache)
# - Native code (written in Zig)

bunx dan Alat Bawaan

bunx menjalankan paket tanpa unduhan menggunakan biner yang di-cache.

# bunx — like npx but instant (no download delay)
bunx create-next-app@latest my-app
bunx prettier --write .
bunx eslint src/

# Run TypeScript directly
bun index.ts            # works without ts-node or compilation
bun --watch server.ts   # auto-restart on file changes

# Bun's built-in test runner (Jest-compatible API)
bun test
bun test --watch
bun test src/utils.test.ts

// Example test file
import { expect, test, describe } from 'bun:test';

describe('math', () => {
    test('adds numbers', () => {
        expect(1 + 2).toBe(3);
    });

    test('async operations', async () => {
        const result = await fetch('https://api.example.com/data');
        expect(result.ok).toBe(true);
    });
});

Skrip dan Integrasi Build

Bun menjalankan skrip package.json lebih cepat dari npm/yarn.

// package.json scripts with Bun
{
  "scripts": {
    "dev": "bun --watch src/index.ts",
    "build": "bun build src/index.ts --outdir dist --target node",
    "bundle:web": "bun build src/app.ts --outdir public/js --target browser --minify",
    "test": "bun test",
    "lint": "bunx eslint src/",
    "format": "bunx prettier --write src/"
  }
}

// Bun's built-in bundler (replaces esbuild/webpack for simple cases)
// bun build src/index.ts --outdir dist --target node --minify

// Environment variables (Bun auto-loads .env files)
// No need for dotenv package!
console.log(process.env.DATABASE_URL); // works in Bun automatically

Bun vs npm vs pnpm vs Yarn

FeatureBunnpmpnpmYarn
Install speed (warm)1.2s28.3s9.4s14.1s
Lockfile formatBinaryJSONYAMLCustom text
node_modules styleSymlinksHoistedSymlinksHoisted/PnP
Workspace supportYesYesYesYes
Built-in TypeScriptYes (native)NoNoNo
Built-in test runnerYes (Jest API)NoNoNo
.env auto-loadingYesNoNoNo

Praktik Terbaik

  • Commit bun.lockb untuk instalasi yang dapat direproduksi.
  • Gunakan bun --watch untuk pengembangan.
  • Migrasi secara bertahap dari npm.
  • Untuk monorepo, gabungkan Bun workspaces dengan Turborepo.
  • Bun memuat .env secara otomatis — tidak perlu paket dotenv.

Pertanyaan yang Sering Diajukan

Apakah Bun sudah siap produksi di 2026?

Ya. Bun 1.0+ mencapai stabilitas pada 2023 dengan cakupan API Node.js 99%+.

Apakah Bun bekerja dengan semua paket npm?

Hampir semua. Pengecualian untuk paket yang menggunakan addon Node natif.

Bisakah saya menggunakan Bun dengan Next.js?

Ya untuk package manager — bun install langsung bekerja.

Bagaimana bun.lockb berbeda dari package-lock.json?

bun.lockb adalah format biner yang di-parse dalam milidetik.

Bun atau pnpm untuk proyek baru?

Keduanya sangat baik. Bun untuk pendekatan all-in-one, pnpm untuk monorepo besar.

Apakah ini membantu?

Stay Updated

Get weekly dev tips and new tool announcements.

No spam. Unsubscribe anytime.

Partner Picks

Sponsor this article

Place your product next to this developer topic with tracked clicks.

Ask about article sponsorship

This site uses cookies for analytics and to display ads. By continuing to browse, you agree. Privacy Policy