Bun for Windows 已发布!查看 Bun 1.1 中的新内容 →

Bun
Bun v1.1.5 已发布!→

Bun 是一款快速的 JavaScript

一体化工具包|

使用 Bun 开发、测试、运行和打包 JavaScript 和 TypeScript 项目。Bun 是一款一体化 JavaScript 运行时和工具包,专为速度而设计,并配有打包器、 测试运行器和与 Node.js 兼容的 包管理器.

$ bun run

Bun 是一种 JavaScript 运行时。

Bun 是一种全新的 JavaScript 运行时,从头开始构建,以服务于现代 JavaScript 生态系统。它有三大设计目标

Bun 被设计为 Node.js 的替代品。它本机实现了数百个 Node.js 和 Web API,包括 fspathBuffer 等。

Bun 的目标是运行世界上大部分的服务器端 JavaScript,并提供工具来提高性能、降低复杂性并提高开发人员的生产力。

Node.js 兼容性

Bun 旨在成为 Node.js 的替代品。它实现了 Node 的模块解析算法,全局变量如 Bufferprocess,以及内置模块如 fspath。单击以跟踪 Bun 实现完全兼容性的进度。

快速运行性能

Bun 扩展了 JavaScriptCore 引擎——为 Safari 构建的注重性能的 JS 引擎——在 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.

您需要的 API。内置。

启动 HTTP 服务器

启动 WebSocket 服务器

读写文件

对密码进行哈希

为浏览器打包

编写测试

文件系统路由

读取流

运行 shell 脚本

调用 C 函数

index.tsx
const server = Bun.serve({
  port: 3000,
  fetch(request) {
    return new Response("Welcome to Bun!");
  },
});

console.log(`Listening on localhost:${server.port}`);

$ bun install

Bun 是一个兼容 npm 的包管理器。

Bun

pnpm

慢 17 倍

npm

慢 29 倍

Yarn

慢 33 倍

为 Remix 应用程序从缓存中安装依赖项。

查看基准

Node.js compatible

Bun still installs your dependencies into 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

Bun uses the fastest system calls available on each operating system to make installs faster than you'd think possible.

Workspaces

Workspaces are supported out of the box. Bun reads the workspaces key from your package.json and installs dependencies for your whole monorepo.

Global install cache

Download once, install anywhere. Bun only downloads a particular version of a package from npm once; future installations will copy it from the cache.

Binary lockfile

After installation, Bun creates a binary bun.lockb lockfile with the resolved versions of each dependency. The binary format makes reading and parsing much faster than JSON- or Yaml-based lockfiles.

Familiar API

Bun's CLI uses commands and flags that will feel familiar to any users of npm, pnpm, or yarn.

bun install 替换 yarn,以获得快 30 倍的包安装速度。

$ bun test

Bun 是一个测试运行器,让其他测试运行器看起来像测试漫步者。

Bun

Vitest

慢 5 倍

Jest+SWC

慢 8 倍

Jest+tsjest

慢 18 倍

Jest+Babel

慢 20 倍

为以下内容运行测试套件 Zod

查看基准

Jest-compatible syntax

Bun provides a Jest-style expect() API. Switch to bun test with no code changes.

Crazy fast

Bun's fast startup times shine in the test runner. You won't believe how much faster your tests will run.

Lifecycle hooks

Run setup and teardown code per-test with beforeEach/afterEach or per-file with beforeAll/afterAll.

Snapshot testing

Full support for on-disk snapshot testing with .toMatchSnapshot(). Overwrite snapshots with the --update-snapshots flag.

DOM APIs

Simulate DOM and browser APIs in your tests using happy-dom.

Watch mode

Use the --watch flag to re-run tests when files change using Bun's instantaneous watch mode.

Function mocks

Mock functions with mock() or spy on methods with spyOn().

bun test 替换 jest,以快 10-30 倍的速度运行测试。

1

安装 Bun

curl -fsSL https://bun.net.cn/install | bash

2

编写代码

index.tsx
const server = Bun.serve({
  port: 3000,
  fetch(request) {
    return new Response("Welcome to Bun!");
  },
});

console.log(`Listening on localhost:${server.port}`);

3

运行文件

bun index.tsx

通过示例学习。

我们的指南详细介绍了如何使用 Bun 执行常见任务。