Skip to content

Bun チートシート

高速なオールインワンの JavaScript ランタイム、バンドラ、パッケージマネージャ。

01

はじめに

Hello World

Bun は Zig 製ランタイムで JS/TS をネイティブに実行します。

bun
console.log("Hello, World!");

実行

Bun は node のドロップイン代替として使えます。

bun
# bun run main.ts
# bun main.ts

サーバー

Bun.serve で HTTP サーバーを立てられます。

bun
export default {
  port: 3000,
  fetch(req) {
    return new Response("Hello!");
  }
};
02

API

ファイル I/O

Bun.file で高速にファイルを読み込めます。

bun
const text = await Bun.file("file.txt").text();
const buf = await Bun.file("data.bin").arrayBuffer();

SQLite

SQLite サポートが組み込まれています。

bun
import { Database } from "bun:sqlite";
const db = new Database("mydb.sqlite");
const query = db.query("SELECT * FROM users");
console.log(query.all());

ハッシュ化

パスワードのハッシュ化機能も組み込まれています。

bun
const hash = await Bun.password.hash("secret");
const ok = await Bun.password.verify("secret", hash);

Was this helpful?