| Nov | DEC | Jan |
| 22 | ||
| 2023 | 2024 | 2025 |
COLLECTED BY
Collection: Certificate Transparency
curl -fsSL https://bun.sh/install | bash
fs, path, Buffer and more.
The goal of Bun is to run most of the world's server-side JavaScript and provide tools to improve performance, reduce complexity, and multiply developer productivity.
Drop-in Node.js compatibility
Bun aims to be a drop-in replacement for Node.js. It implements Node's module resolution algorithm, globals like Buffer and process, and built-in modules like fsand path. Click to track Bun's progress towards full compatibility.
Fast running performance
Bun extends the JavaScriptCore engine—the performance-minded JS engine built for Safari—with native-speed functionality implemented in Zig.
Works with node_modules
With Bun, you still use package.json to manage your dependencies. Use Bun's native npm client to see just how fast installing dependencies can be.
No more module madness
Forget the complicated rules around CommonJS, ESM, file extensions, resolution priority, and package.json configurations. With Bun, it just works.
TypeScript
TypeScript is a first-class citizen in Bun. Directly execute .ts and .tsx files. Bun respects your settings configured in tsconfig.json, including "paths", "jsx", and more.
Web-standard APIs
Bun implements the Web-standard APIs you know and love, including fetch, ReadableStream, Request, Response, WebSocket, and FormData.
JSX
JSX just works. Bun internally transpiles JSX syntax to vanilla JavaScript. Like TypeScript itself, Bun assumes React by default but respects custom JSX transforms defined in tsconfig.json.
Watch mode
The bun run CLI provides a smart --watch flag that automatically restarts the process when any imported file changes.
Cross-platform shell scripts
The Bun.$ API implements a cross-platform bash-like interpreter, shell, and coreutils. This makes it easy to run shell scripts from JavaScript for devops tasks.
const server = Bun.serve({
port: 3000,
fetch(request) {
return new Response("Welcome to Bun!");
},
});
console.log(`Listening on localhost:${server.port}`);
index.tsx
const server = Bun.serve<{ authToken: string; }>({
fetch(req, server) {
// use a library to parse cookies
const cookies = parseCookies(req.headers.get("Cookie"));
server.upgrade(req, {
data: { authToken: cookies['X-Token'] },
});
},
websocket: {
// handler called when a message is received
async message(ws, message) {
console.log(`Received: ${message}`);
const user = getUserFromToken(ws.data.authToken);
await db.Message.insert({
message: String(message),
userId: user.id,
});
},
},
});
console.log(`Listening on localhost:${server.port}`);
index.tsx
const file = Bun.file(import.meta.dir + '/package.json'); // BunFile
const pkg = await file.json(); // BunFile extends Blob
pkg.name = 'my-package';
pkg.version = '1.0.0';
await Bun.write(file, JSON.stringify(pkg, null, 2));
index.tsx
const password = "super-secure-pa$$word";
const hash = await Bun.password.hash(password);
// => $argon2id$v=19$m=65536,t=2,p=1$tFq+9AVr1bfPxQdh...
const isMatch = await Bun.password.verify(password, hash);
// => true
bundle.tsx
await Bun.build({
entrypoints: ["./index.tsx"],
outdir: "./build",
minify: true,
plugins: [ /* ... */ ]
})
index.tsx
import { describe, expect, test, beforeAll } from "bun:test";
beforeAll(() => {
// setup tests
});
describe("math", () => {
test("addition", () => {
expect(2 + 2).toBe(4);
expect(7 + 13).toMatchSnapshot();
});
});
index.tsx
const router = new Bun.FileSystemRouter({
style: "nextjs",
dir: "/path/to/pages"
});
const match = router.match("/blog/my-cool-post");
match.filePath; // "/path/to/pages/blog/[slug].tsx",
match.kind; // "dynamic"
match.params; // { slug: "my-cool-post" }
index.tsx
import { Database } from "bun:sqlite";
constdb= new Database("db.sqlite");
console.log(db.query("SELECT 1 as x").get());
// { x: 1 }
index.tsx
import { $ } from 'bun';
// Run a shell command (also works on Windows!)
await $`echo "Hello, world!"`;
const response = await fetch("https://example.com");
// Pipe the response body to gzip
const data = await $`gzip < ${response}`.arrayBuffer();
index.tsx
import { dlopen, FFIType, suffix } from "bun:ffi";
// `suffix` is either "dylib", "so", or "dll" depending on the platform
const path = `libsqlite3.${suffix}`;
const {
symbols: {
sqlite3_libversion, // the function to call
},
} = dlopen(path, {
sqlite3_libversion: {
args: [], // no arguments
returns: FFIType.cstring, // returns a string
},
});
console.log(`SQLite 3 version: ${sqlite3_libversion()}`);
$ bun install
Bun
pnpm
npm
Yarn
Installing dependencies from cache for a Remix app.
View benchmark
Node.js compatible
node_modules like npm and other package managers—it just does it faster. You don't need to use the Bun runtime to use Bun as a package manager.Crazy fast
Workspaces
workspaces key from your package.json and installs dependencies for your whole monorepo.Global install cache
Security by default
postinstall scripts by default. Popular packages are automatically allow-listed; others can be added to the trustedDependencies in your package.json. Cross-platform package.json scripts
cross-env, rimraf and node-which.Familiar API
npm, pnpm, or yarn.Reads .npmrc & package-lock.json
$ bun test
Bun
Vitest
Jest+SWC
Jest+tsjest
Jest+Babel
Running the test suite for Zod
View benchmark
Jest-compatible syntax
expect() API. Switch to bun test with no code changes.Crazy fast
Lifecycle hooks
beforeEach/afterEach or per-file with beforeAll/afterAll.ESM, TypeScript & JSX just work
Snapshot testing
.toMatchSnapshot(). Overwrite snapshots with the --update-snapshots flag.DOM APIs
Watch mode
--watch flag to re-run tests when files change using Bun's instantaneous watch mode.Function mocks
mock() or spy on methods with spyOn().1
Install Bun
curl -fsSL https://bun.sh/install | bashconst server = Bun.serve({
port: 3000,
fetch(request) {
return new Response("Welcome to Bun!");
},
});
console.log(`Listening on localhost:${server.port}`);
3
Run the file
bun index.tsx
bun test
Spy on methods in bun test
Using Testing Library with Bun
Update snapshots in bun test
Run tests in watch mode with Bun
Use snapshot testing in bun test
Bail early with the Bun test runner
Skip tests with the Bun test runner
Migrate from Jest to Bun's test runner
Run your tests with the Bun test runner
Set the system time in Bun's test runner
Write browser DOM tests with Bun and happy-dom
Set a per-test timeout with the Bun test runner
Mark a test as a "todo" with the Bun test runner
Re-run tests multiple times with the Bun test runner
Set a code coverage threshold with the Bun test runner
Generate code coverage reports with the Bun test runner
import, require, and test Svelte components with bun test
Utilities
Hash a password
Escape an HTML string
Get the current Bun version
Encode and decode base64 strings
Check if two objects are deeply equal
Detect when code is executed with Bun
Get the directory of the current file
Get the file name of the current file
Convert a file URL to an absolute path
Compress and decompress data with gzip
Convert an absolute path to a file URL
Get the path to an executable bin file
Sleep for a fixed number of milliseconds
Compress and decompress data with DEFLATE
Get the absolute path of the current file
Check if the current file is the entrypoint
Get the absolute path to the current entrypoint
WebSocket
Build a simple WebSocket server
Enable compression for WebSocket messages
Build a publish-subscribe WebSocket server
Set per-socket contextual data on a WebSocket
Writing files
Delete a file
Write to stdout
Write a Blob to a file
Write a file to stdout
Append content to a file
Write a string to a file
Write a file incrementally
Write a Response to a file
Copy a file to another location
Write a ReadableStream to a file