bun – oven-sh
极快的 JavaScript 运行时、打包器、测试运行器和包管理器 – 集于一身
关键指标一览
主题标签
README 详细介绍
Bun
Read the docs →
What is Bun?
Bun is an all-in-one toolkit for JavaScript and TypeScript apps. It ships as a single executable called bun.
At its core is the _Bun runtime_, a fast JavaScript runtime designed as a drop-in replacement for Node.js. It's written in Rust and powered by JavaScriptCore under the hood, dramatically reducing startup times and memory usage.
bun run index.tsx # TS and JSX supported out-of-the-box
The bun command-line tool also implements a test runner, script runner, and Node.js-compatible package manager. Instead of 1,000 node_modules for development, you only need bun. Bun's built-in tools are significantly faster than existing options and usable in existing Node.js projects with little to no changes.
bun test # run tests
bun run start # run the `start` script in `package.json`
bun install <pkg> # install a package
bunx cowsay 'Hello, world!' # execute a package
Install
Bun supports Linux (x64 & arm64), macOS (x64 & Apple Silicon), and Windows (x64 & arm64).
> Linux users — Kernel version 5.6 or higher is strongly recommended, but the minimum is 5.1.
> x64 users — if you see "illegal instruction" or similar errors, check our CPU requirements
# with install script (recommended)
curl -fsSL https://bun.com/install | bash
# on windows
powershell -c "irm bun.sh/install.ps1 | iex"
# with npm
npm install -g bun
# with Homebrew
brew tap oven-sh/bun
brew install bun
# with Docker
docker pull oven/bun
docker run --rm --init --ulimit memlock=-1:-1 oven/bun
Upgrade
To upgrade to the latest version of Bun, run:
bun upgrade
Bun automatically releases a canary build on every commit to main. To upgrade to the latest canary build, run:
bun upgrade --canary
Quick links
- Runtime
- <code class="ra0-md-code">bun run</code>
- File types (Loaders)
- JSX
- Environment variables
- Bun APIs
- Web APIs
- Node.js compatibility
- Plugins
- Watch mode / Hot Reloading
- Module resolution
- Auto-install
- bunfig.toml
- Debugger
- REPL
- $ Shell
- Package manager
- <code class="ra0-md-code">bun install</code>
- <code class="ra0-md-code">bun add</code>
- <code class="ra0-md-code">bun remove</code>
- <code class="ra0-md-code">bun update</code>
- <code class="ra0-md-code">bun link</code>
- <code class="ra0-md-code">bun pm</code>
- <code class="ra0-md-code">bun outdated</code>
- <code class="ra0-md-code">bun publish</code>
- <code class="ra0-md-code">bun patch</code>
- <code class="ra0-md-code">bun why</code>
- <code class="ra0-md-code">bun audit</code>
- <code class="ra0-md-code">bun info</code>
- Global cache
- Global store
- Isolated installs
- Workspaces
- Catalogs
- Lifecycle scripts
- Filter
- Lockfile
- Scopes and registries
- Overrides and resolutions
- Security scanner API
- <code class="ra0-md-code">.npmrc</code>
- Bundler
- <code class="ra0-md-code">Bun.build</code>
- Loaders
- Plugins
- Macros
- vs esbuild
- Single-file executable
- CSS
- HTML & static sites
- Hot Module Replacement (HMR)
- Full-stack with HTML imports
- Standalone HTML
- Bytecode caching
- Minifier
- Test runner
- <code class="ra0-md-code">bun test</code>
- Writing tests
- Lifecycle hooks
- Mocks
- Snapshots
- Dates and times
- DOM testing
- Code coverage
- Configuration
- Discovery
- Reporters
- Runtime Behavior
- Package runner
- <code class="ra0-md-code">bunx</code>
- API
- HTTP server (<code class="ra0-md-code">Bun.serve</code>)
- HTTP routing
- HTTP error handling
- HTTP metrics
- WebSockets
- Workers
- Binary data
- Streams
- File I/O (<code class="ra0-md-code">Bun.file</code>)
- Archive (tar)
- SQLite (<code class="ra0-md-code">bun:sqlite</code>)
- PostgreSQL (<code class="ra0-md-code">Bun.sql</code>)
- Redis (<code class="ra0-md-code">Bun.redis</code>)
- S3 Client (<code class="ra0-md-code">Bun.s3</code>)
- FileSystemRouter
- TCP sockets
- UDP sockets
- Globals
- Child processes (spawn)
- Cron (<code class="ra0-md-code">Bun.cron</code>)
- WebView
- Transpiler (<code class="ra0-md-code">Bun.Transpiler</code>)
- Hashing
- Colors (<code class="ra0-md-code">Bun.color</code>)
- Console
- FFI (<code class="ra0-md-code">bun:ffi</code>)
- C Compiler (<code class="ra0-md-code">bun:ffi</code> cc)
- HTMLRewriter
- Cookies (<code class="ra0-md-code">Bun.Cookie</code>)
- CSRF (<code class="ra0-md-code">Bun.CSRF</code>)
- Secrets (<code class="ra0-md-code">Bun.secrets</code>)
- YAML (<code class="ra0-md-code">Bun.YAML</code>)
- TOML (<code class="ra0-md-code">Bun.TOML</code>)
- JSON5
- JSONL
- Markdown
- Image processing
- Utils
- Node-API
- Glob (<code class="ra0-md-code">Bun.Glob</code>)
- Semver (<code class="ra0-md-code">Bun.semver</code>)
- DNS
- fetch API extensions
Guides
- Deployment
- Deploy to Vercel
- Deploy to Railway
- Deploy to Render
- Deploy to AWS Lambda
- Deploy to DigitalOcean
- Deploy to Google Cloud Run
- Binary
- Convert a Blob to a string
- Convert a Buffer to a blob
- Convert a Blob to a DataView
- Convert a Buffer to a string
- Convert a Blob to a ReadableStream
- Convert a Blob to a Uint8Array
- Convert a DataView to a string
- Convert a Uint8Array to a Blob
- Convert a Blob to an ArrayBuffer
- Convert an ArrayBuffer to a Blob
- Convert a Buffer to a Uint8Array
- Convert a Uint8Array to a Buffer
- Convert a Uint8Array to a string
- Convert a Buffer to an ArrayBuffer
- Convert an ArrayBuffer to a Buffer
- Convert an ArrayBuffer to a string
- Convert a Uint8Array to a DataView
- Convert a Buffer to a ReadableStream
- Convert a Uint8Array to an ArrayBuffer
- Convert an ArrayBuffer to a Uint8Array
- Convert an ArrayBuffer to an array of numbers
- Convert a Uint8Array to a ReadableStream
- Ecosystem
- Use React and JSX
- Use Gel with Bun
- Use Prisma with Bun
- Use Prisma Postgres with Bun
- Add Sentry to a Bun app
- Create a Discord bot
- Run Bun as a daemon with PM2
- Use Drizzle ORM with Bun
- Use Upstash Redis with Bun
- Build an app with Nuxt and Bun
- Build an app with Qwik and Bun
- Build an app with Astro and Bun
- Build an app with Remix and Bun
- Build a frontend using Vite and Bun
- Build an app with Next.js and Bun
- Run Bun as a daemon with systemd
- Build an HTTP server using Hono and Bun
- Build an app with SvelteKit and Bun
- Build an app with SolidStart and Bun
- Build an app with TanStack Start and Bun
- Build an HTTP server using Elysia and Bun
- Build an HTTP server using StricJS and Bun
- Containerize a Bun application with Docker
- Build an HTTP server using Express and Bun
- Use Neon Postgres through Drizzle ORM
- Server-side render (SSR) a React component
- Read and write data to MongoDB using Mongoose and Bun
- Use Neon's Serverless Postgres with Bun
- HTMLRewriter
- Extract links from a webpage using HTMLRewriter
- Extract social share images and Open Graph tags
- HTTP
- Hot reload an HTTP server
- Common HTTP server usage
- Write a simple HTTP server
- Configure TLS on an HTTP server
- Send an HTTP request using fetch
- Proxy HTTP requests using fetch()
- Start a cluster of HTTP servers
- Stream a file as an HTTP Response
- fetch with unix domain sockets in Bun
- Upload files via HTTP using FormData
- Streaming HTTP Server with Async Iterators
- Streaming HTTP Server with Node.js Streams
- Server-Sent Events (SSE) with Bun
- Install
- Add a dependency
- Add a Git dependency
- Add a peer dependency
- Add a trusted dependency
- Add a development dependency
- Add a tarball dependency
- Add an optional dependency
- Generate a yarn-compatible lockfile
- Configuring a monorepo using workspaces
- Install a package under a different name
- Install dependencies with Bun in GitHub Actions
- Using bun install with Artifactory
- Configure git to diff Bun's lockb lockfile
- Override the default npm registry for bun install
- Using bun install with an Azure Artifacts npm registry
- Migrate from npm install to bun install
- Configure a private registry for an organization scope with bun install
- Process
- Read from stdin
- Listen for CTRL+C
- Spawn a child process
- Listen to OS signals
- Parse command-line arguments
- Read stderr from a child process
- Read stdout from a child process
- Get the process uptime in nanoseconds
- Spawn a child process and communicate using IPC
- Read file
- Read a JSON file
- Check if a file exists
- Read a file as a string
- Read a file to a Buffer
- Get the MIME type of a file
- Watch a directory for changes
- Read a file as a ReadableStream
- Read a file to a Uint8Array
- Read a file to an ArrayBuffer
- Runtime
- Delete files
- Run a Shell Command
- Import a JSON file
- Import a TOML file
- Import a YAML file
- Import a JSON5 file
- Set a time zone in Bun
- Set environment variables
- Re-map import paths
- Delete directories
- Read environment variables
- Import a HTML file as text
- Install and run Bun in GitHub Actions
- Debugging Bun with the web debugger
- Install TypeScript declarations for Bun
- Debugging Bun with the VS Code extension
- Inspect memory usage using V8 heap snapshots
- Define and replace static globals & constants
- Build-time constants with --define
- Codesign a single-file JavaScript executable on macOS
- Streams
- Convert a ReadableStream to JSON
- Convert a ReadableStream to a Blob
- Convert a ReadableStream to a Buffer
- Convert a ReadableStream to a string
- Convert a ReadableStream to a Uint8Array
- Convert a ReadableStream to an array of chunks
- Convert a Node.js Readable to JSON
- Convert a ReadableStream to an ArrayBuffer
- Convert a Node.js Readable to a Blob
- Convert a Node.js Readable to a string
- Convert a Node.js Readable to an Uint8Array
- Convert a Node.js Readable to an ArrayBuffer
- Test
- Spy on methods in <code class="ra0-md-code">bun test</code>
- Bail early with the Bun test runner
- Mock functions in <code class="ra0-md-code">bun test</code>
- Run tests in watch mode with Bun
- Use snapshot testing in <code class="ra0-md-code">bun test</code>
- Skip tests with the Bun test runner
- Using Testing Library with Bun
- Update snapshots in <code class="ra0-md-code">bun test</code>
- Run your tests with the Bun test runner
- Set the system time in Bun's test runner
- Set a per-test timeout with the Bun test runner
- Migrate from Jest to Bun's test runner
- Write browser DOM tests with Bun and happy-dom
- Mark a test as a "todo" with the Bun test runner
- Re-run tests multiple times with the Bun test runner
- Generate code coverage reports with the Bun test runner
- import, require, and test Svelte components with bun test
- Set a code coverage threshold with the Bun test runner
- Selectively run tests concurrently with glob patterns
- Util
- Generate a UUID
- Hash a password
- Escape an HTML string
- Get the current Bun version
- Upgrade Bun to the latest version
- Encode and decode base64 strings
- Compress and decompress data with gzip
- Sleep for a fixed number of milliseconds
- Detect when code is executed with Bun
- Check if two objects are deeply equal
- Compress and decompress data with DEFLATE
- Get the absolute path to the current entrypoint
- Get the directory of the current file
- Check if the current file is the entrypoint
- Get the file name of the current file
- Convert a file URL to an absolute path
- Convert an absolute path to a file URL
- Get the absolute path of the current file
- Get the path to an executable bin file
- WebSocket
- Build a publish-subscribe WebSocket server
- Build a simple WebSocket server
- Enable compression for WebSocket messages
- Set per-socket contextual data on a WebSocket
- Write file
- Delete a file
- Write to stdout
- Write a file to stdout
- Write a Blob to a file
- Write a string to a file
- Append content 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
Contributing
Refer to the Project > Contributing guide to start contributing to Bun.
License
Refer to the Project > License page for information about Bun's licensing.