What is Bun?
Bun is an all-in-one JavaScript/TypeScript toolkit that includes a runtime, package manager, test runner, and bundler. It's designed to be a drop-in replacement for Node.js with significantly faster performance.
Simply its a,
Installation
macOS & Linux
curl -fsSL https://bun.sh/install | bash
Windows (PowerShell)
powershell -c "irm bun.sh/install.ps1|iex"
Using npm (Works everywhere)
npm install -g bun
Docker
docker pull oven/bun
docker run --rm --init --ulimit memlock=-1:-1 oven/bun
Verify Installation
bun --version
Upgrade Bun
bun upgrade
Project Initialization
Create a React App
Create a new React app
bun init --reactRun in development mode
bun devBuild for production
bun run buildRun production server
bun start
Create a Next.js App
Create a new Next.js app
bun create next-app@latest my-appNavigate to project
cd my-appStart development server with Bun runtime
bun --bun run dev
Update package.json scripts for Next.js:
{
"scripts": {
"dev": "bun --bun next dev",
"build": "bun --bun next build",
"start": "bun --bun next start"
}
}
Create Other Projects
Create from a GitHub template
bun create / Examples
bun create elysia my-elysia-app
bun create hono my-hono-app
Package Management
Install Dependencies
Install all dependencies from package.json
bun installInstall with verbose logging
bun install --verboseInstall silently (no output)
bun install --silentProduction install (no devDependencies)
bun install --production
Add Packages
Add a dependency
bun add Add specific version
bun add react@18.2.0Add latest version
bun add react@latestAdd as dev dependency
bun add -d
bun add --dev typescriptAdd as exact version (no ^ prefix)
bun add zod --exact
Remove Packages
bun remove
Update Packages
Check for outdated packages
bun outdatedUpdate all packages
bun update
Common Package Examples
React ecosystem
bun add react react-dom
bun add -d @types/react @types/react-domNext.js
bun add nextTailwind CSS
bun add -d tailwindcss postcss autoprefixer
bunx tailwindcss init -pTypeScript
bun add -d typescript @types/nodeTesting libraries
bun add -d @testing-library/react jestPopular utilities
bun add zod axios lodash
Running Scripts
Run Files
Run a JavaScript/TypeScript file
bun run index.ts
bun run index.js
bun run index.tsxShorthand (without 'run')
bun index.ts
Run package.json Scripts
Run a script from package.json
bun run dev
bun run build
bun run startRun with Bun runtime (instead of Node)
bun --bun run dev
Watch Mode
Run with auto-reload on file changes
bun --watch run index.ts
Execute Packages (bunx)
Execute a package without installing globally
bunx cowsay "Hello, Bun!"Create React App alternative
bunx create-react-app my-appRun Tailwind CLI
bunx tailwindcss -i ./src/input.css -o ./dist/output.css
Testing
Run Tests
Run all tests
bun testRun specific test file
bun test ./test/specific-file.test.tsRun tests matching a pattern
bun test --test-name-pattern "addition"Watch mode
bun test --watch
Test File Naming
Bun automatically detects test files with these patterns:
*.test.{js|jsx|ts|tsx}*_test.{js|jsx|ts|tsx}*.spec.{js|jsx|ts|tsx}*_spec.{js|jsx|ts|tsx}Writing Tests
import { expect, test, describe } from "bun:test";describe("math operations", () => {
test("2 + 2 equals 4", () => {
expect(2 + 2).toBe(4);
});
test("arrays match", () => {
expect([1, 2, 3]).toEqual([1, 2, 3]);
});
});
Bundling
Basic Bundle
Bundle for browser
bun build ./index.tsx --outdir ./outBundle with minification
bun build ./index.tsx --outdir ./out --minifyWatch mode
bun build ./index.tsx --outdir ./out --watch
Bundle Options
Bundle for different targets
bun build ./index.ts --target browser
bun build ./index.ts --target bun
bun build ./index.ts --target nodeGenerate source maps
bun build ./index.tsx --outdir ./out --sourcemapCode splitting
bun build ./index.tsx --outdir ./out --splitting
Plugins
Bun supports plugins for extending bundler functionality. Plugins can be configured in bunfig.toml.
TailwindCSS Plugin
Bun has a built-in TailwindCSS plugin for seamless integration:
Install TailwindCSS and the Bun plugin
bun add tailwindcss bun-plugin-tailwind
Configure in bunfig.toml:
[serve.static]
plugins = ["bun-plugin-tailwind"]
Use in HTML (add to head):
Add this link tag to your HTML head section
<link rel="stylesheet" href="tailwindcss" />
Or import in CSS:
@import "tailwindcss";.custom-class {
@apply bg-red-500 text-white;
}
Custom Plugins
You can create custom plugins by implementing the BunPlugin interface:
bunfig.toml
[serve.static]
plugins = ["./my-plugin.ts"]
// my-plugin.ts
import type { BunPlugin } from "bun";const myPlugin: BunPlugin = {
name: "my-custom-plugin",
setup(build) {
build.onLoad({ filter: /\.custom$/ }, async (args) => {
const text = await Bun.file(args.path).text();
return {
contents: "export default " + JSON.stringify(text) + ";",
loader: "js",
};
});
},
};
export default myPlugin;
Environment Variables
Bun automatically loads .env files
Create .env file and access via process.env or Bun.env
Access in code
console.log(Bun.env.MY_VARIABLE);
console.log(process.env.MY_VARIABLE);
Quick Commands Reference
| Command | Description |
|---|---|
bun init | Initialize a new project |
bun init --react | Create a React project |
bun install | Install dependencies |
bun add | Add a package |
bun add -d | Add dev dependency |
bun remove | Remove a package |
bun run | Run a script |
bun test | Run tests |
bun build | Bundle your project |
bunx | Execute a package |
bun upgrade | Upgrade Bun |
Performance Comparison
Bun is significantly faster than npm/yarn for package installation: