Bun 安装

本文介绍了Bun,一个专注于速度、优雅API和开发者体验的新型JavaScript运行时,它替代Node.js,支持多种功能如快速启动、HTTP服务器、文件操作等。文章详细讲解了Bun的特性、官网地址以及安装步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


开发、测试、运行和捆绑 JavaScript 和 TypeScript 项目 - 全部使用 Bun。

1. 什么是 Bun

Bun 是一个从头开始构建的新 JavaScript 运行时,旨在服务现代 JavaScript 生态系统。它有三个主要设计目标:

  • 速度:Bun 启动快,运行也快。它扩展了 JavaScriptCore,即为 Safari 构建的注重性能的 JS 引擎。随着计算向边缘发展,这一点至关重要。
  • 优雅的 API:Bun 提供了一组最小的高度优化的 API,用于执行常见任务,例如启动 HTTP 服务器和写入文件。
  • 凝聚力 DX:Bun 是一个用于构建 JavaScript 应用程序的完整工具包,包括包管理器、测试运行器和捆绑器。

Bun 被设计为 Node.js 的直接替代品。它原生实现了数百个 Node.js 和 Web API,包括 fs 、 path 、 Buffer 等。

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

2. Bun 官网地址

https://bun.sh/

3. Bun 安装

运行下面命令进行安装,

# 支持在 macOS、Linux 和 WSL 上安装
curl -fsSL https://bun.sh/install | bash
source /root/.bashrc

运行下面命令进行确认,

bun --help

输出日志,

Bun is a fast JavaScript runtime, package manager, bundler, and test runner. (1.0.20 (09d51486))

Usage: bun <command> [...flags] [...args]

Commands:
  run       ./my-script.ts       Execute a file with Bun
            lint                 Run a package.json script
  test                           Run unit tests with Bun
  x         prettier             Execute a package binary (CLI), installing if needed (bunx)
  repl                           Start a REPL session with Bun

  install                        Install dependencies for a package.json (bun i)
  add       next-app             Add a dependency to package.json (bun a)
  remove    react                Remove a dependency from package.json (bun rm)
  update    webpack              Update outdated dependencies
  link      [<package>]          Register or link a local npm package
  unlink                         Unregister a local npm package
  pm <subcommand>                Additional package management utilities

  build     ./a.ts ./b.jsx       Bundle TypeScript & JavaScript into a single file

  init                           Start an empty Bun project from a blank template
  create    lyra                 Create a new project from a template (bun c)
  upgrade                        Upgrade to latest version of Bun.
  <command> --help               Print help text for command.

Flags:
      --watch                    Automatically restart the process on file change
      --hot                      Enable auto reload in the Bun runtime, test runner, or bundler
      --smol                     Use less memory, but run garbage collection more often
  -r, --preload                  Import a module before other modules are loaded
      --inspect                  Activate Bun's debugger
      --inspect-wait             Activate Bun's debugger, wait for a connection before executing
      --inspect-brk              Activate Bun's debugger, set breakpoint on first line of code and wait
      --if-present               Exit without an error if the entrypoint does not exist
      --no-install               Disable auto install in the Bun runtime
      --install                  Configure auto-install behavior. One of "auto" (default, auto-installs when no node_modules), "fallback" (missing packages only), "force" (always).
  -i                             Auto-install dependencies during execution. Equivalent to --install=fallback.
  -e, --eval                     Evaluate argument as a script
      --prefer-offline           Skip staleness checks for packages in the Bun runtime and resolve from disk
      --prefer-latest            Use the latest matching versions of packages in the Bun runtime, always checking npm
  -p, --port                     Set the default port for Bun.serve
  -b, --bun                      Force a script or package to use Bun's runtime instead of Node.js (via symlinking node)      --silent                   Don't print the script command
  -v, --version                  Print version and exit
      --revision                 Print version with revision and exit
      --env-file                 Load environment variables from the specified file(s)
      --cwd                      Absolute path to resolve files & entry points from. This just changes the process' cwd.  -c, --config                   Specify path to Bun config file. Default $cwd/bunfig.toml
  -h, --help                     Display this menu and exit

(more flags in bun install --help, bun test --help, and bun build --help)

Learn more about Bun:            https://bun.sh/docs
Join our Discord community:      https://bun.sh/discord

完结!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值