Skip to content

bun run CLI 用法

bash
bun run <file or script>

常規執行選項

--silentboolean
不打印腳本命令
--if-presentboolean
如果入口點不存在則無錯誤退出
--evalstring
將參數作為腳本執行。別名:-e
--printstring
將參數作為腳本執行並打印結果。別名:-p
--helpboolean
顯示此菜單並退出。別名:-h

工作空間管理

--elide-linesnumber default: 10
使用 --filter 時顯示的腳本輸出行數(默認:10)。設置為 0 顯示所有行
--filterstring
在匹配模式的所有工作空間包中運行腳本。別名:-F
--workspacesboolean
在所有工作空間包中運行腳本(來自 package.jsonworkspaces 字段)

運行時與進程控制

--bunboolean
強制腳本或包使用 Bun 運行時而不是 Node.js(通過符號鏈接 node)。別名:-b
--shellstring
控制 package.json 腳本使用的 shell。支持 bunsystem
--smolboolean
使用較少內存,但更頻繁地運行垃圾回收
--expose-gcboolean
在全局對象上暴露 gc()。對 Bun.gc() 無影響
--no-deprecationboolean
抑制所有自定義棄用報告
--throw-deprecationboolean
確定棄用警告是否會導致錯誤
--titlestring
設置進程標題
--zero-fill-buffersboolean
強制 Buffer.allocUnsafe(size) 為零填充的布爾值
--no-addonsboolean
如果調用 process.dlopen 則拋出錯誤,並禁用導出條件 node-addons
--unhandled-rejectionsstring
strictthrowwarnnonewarn-with-error-code 之一
--console-depthnumber default: 2
設置 console.log 對象檢查的默認深度(默認:2)

開發工作流

--watchboolean
文件更改時自動重啟進程
--hotboolean
在 Bun 運行時、測試運行器或打包器中啟用自動重載
--no-clear-screenboolean
啟用 --hot 或 --watch 時禁用重載時清除終端屏幕

調試

--inspectstring
激活 Bun 的調試器
--inspect-waitstring
激活 Bun 的調試器,執行前等待連接
--inspect-brkstring
激活 Bun 的調試器,在第一行代碼設置斷點並等待

依賴與模塊解析

--preloadstring
在其他模塊加載之前導入模塊。別名:-r
--requirestring
--preload 的別名,用於 Node.js 兼容性
--importstring
--preload 的別名,用於 Node.js 兼容性
--no-installboolean
禁用 Bun 運行時中的自動安裝
--installstring default: auto
配置自動安裝行為。可選值:auto(默認,沒有 node_modules 時自動安裝)、fallback(僅缺失的包)、force(始終)
-iboolean
執行期間自動安裝依賴。等同於 --install=fallback
--prefer-offlineboolean
跳過 Bun 運行時中包的陳舊檢查並從磁盤解析
--prefer-latestboolean
在 Bun 運行時中使用最新匹配版本的包,始終檢查 npm
--conditionsstring
傳遞自定義條件進行解析
--main-fieldsstring
package.json 中查找的主要字段。默認取決於 --target
--preserve-symlinksboolean
解析文件時保留符號鏈接
--preserve-symlinks-mainboolean
解析主入口點時保留符號鏈接
--extension-orderstring default: .tsx,.ts,.jsx,.js,.json
默認值:.tsx,.ts,.jsx,.js,.json

轉譯與語言功能

--tsconfig-overridestring
指定自定義 tsconfig.json。默認 $cwd/tsconfig.json
--definestring
解析時替換 K:V,例如 --define process.env.NODE_ENV:"development"。值被解析為 JSON。別名:-d
--dropstring
移除函數調用,例如 --drop=console 移除所有 console.* 調用
--loaderstring
使用 .ext:loader 解析文件,例如 --loader .js:jsx。有效加載器:jsjsxtstsxjsontomltextfilewasmnapi。別名:-l
--no-macrosboolean
禁用打包器、轉譯器和運行時中宏的執行
--jsx-factorystring
更改使用經典 JSX 運行時編譯 JSX 元素時調用的函數
--jsx-fragmentstring
更改編譯 JSX 片段時調用的函數
--jsx-import-sourcestring default: react
聲明用於導入 jsx 和 jsxs 工廠函數的模塊說明符。默認:react
--jsx-runtimestring default: automatic
automatic(默認)或 classic
--jsx-side-effectsboolean
將 JSX 元素視為具有副作用(禁用 pure 注釋)
--ignore-dce-annotationsboolean
忽略樹搖優化注釋,如 @__PURE__

網絡與安全

--portnumber
設置 Bun.serve 的默認端口
--fetch-preconnectstring
代碼加載時預連接到 URL
--max-http-header-sizenumber default: 16384
設置 HTTP 請求頭的最大大小(字節)。默認為 16KiB
--dns-result-orderstring default: verbatim
設置 DNS 查找結果的默認順序。有效順序:verbatim(默認)、ipv4firstipv6first
--use-system-caboolean
使用系統的可信證書頒發機構
--use-openssl-caboolean
使用 OpenSSL 的默認 CA 存儲
--use-bundled-caboolean
使用捆綁的 CA 存儲
--redis-preconnectboolean
啟動時預連接到 $REDIS_URL
--sql-preconnectboolean
啟動時預連接到 PostgreSQL
--user-agentstring
設置 HTTP 請求的默認 User-Agent 頭

全局配置與上下文

--env-filestring
從指定文件加載環境變量
--cwdstring
解析文件和入口點的絕對路徑。這只更改進程的 cwd
--configstring
指定 Bun 配置文件路徑。默認 $cwd/bunfig.toml。別名:-c

示例

運行 JavaScript 或 TypeScript 文件:

bash
bun run ./index.js
bun run ./index.tsx

運行 package.json 腳本:

bash
bun run dev
bun run lint

Bun學習網由www.bunjs.com.cn整理維護