Key Features
Native TypeScript
Execute
.ts and .tsx files directly without compilationJSX Support
Built-in JSX/TSX transpilation with React & custom factory support
Fast Transpilation
Zig-powered transpiler with automatic hot reloading
ESM & CommonJS
Seamless interoperability between module systems
Quick Start
Run any JavaScript, TypeScript, JSX, or TSX file:Architecture
Bun’s runtime is built on several core components:Transpiler
The transpiler (src/transpiler.zig) converts TypeScript and JSX to JavaScript on-the-fly:
- TypeScript stripping: Removes type annotations while preserving runtime behavior
- JSX transformation: Converts JSX syntax to function calls
- Modern syntax: Supports decorators, using declarations, and latest TC39 proposals
- Source maps: Generates accurate source maps for debugging
Module Loader
Bun’s module resolution follows Node.js conventions with enhancements:- Package.json resolution: Respects
exports,main,modulefields - Extension handling: Auto-resolves
.ts,.tsx,.jsxextensions - Path mapping: Supports
tsconfig.jsonpaths and baseUrl - Virtual modules: Built-in modules like
bun:ffi,bun:sqlite
Runtime Features
Environment Variables
Environment Variables
Automatic
.env file loading with multiple environment support:.env- Base configuration.env.local- Local overrides (gitignored).env.production- Production-specific.env.development- Development-specific
Watch Mode
Watch Mode
Built-in file watcher for automatic reloading:
- Cross-platform implementation (kqueue on macOS, inotify on Linux)
- Smart debouncing to avoid excessive reloads
- Configurable watch patterns
Hot Module Reloading
Hot Module Reloading
React Fast Refresh and custom HMR support:
- Preserves component state during updates
- Automatic boundary detection
- Framework-agnostic HMR API
Performance
Bun’s runtime achieves exceptional performance through:- JavaScriptCore engine: Apple’s optimized JS engine from WebKit
- Zig implementation: Zero-cost abstractions and manual memory management
- Lazy transpilation: Only transpiles files when imported
- Runtime transpiler cache: Caches transpiled code with hash-based invalidation
- Inline syscalls: Direct system calls without libc overhead
Configuration
Configure runtime behavior withbunfig.toml:
Runtime configuration
Runtime APIs
Bun extends JavaScript with runtime-specific APIs:Runtime APIs
Next Steps
Loaders
Learn about file type loaders and transpilation
TypeScript
TypeScript support and configuration
JSX
JSX/TSX transformation options
Environment Variables
Managing environment configuration
Source Code
Explore the runtime implementation:- Runtime core:
src/runtime.zig- Runtime initialization and features - Transpiler:
src/transpiler.zig- TypeScript/JSX transpilation - JS Parser:
src/js_parser.zig- JavaScript AST parser - Module loader:
src/bun.js/module_loader/- Module resolution - Environment loader:
src/env_loader.zig- .env file parsing