Bun の使い方:インストール、bun init、bun add、npm との run 対照
公式インストール後、init/add/run で dev スクリプトを通し npm と対照。
初めてリポジトリで Bun を使うとき、bun init・bun add・bun run の役割と npm / Node との対応が分からず止まりがちです。
The query is Bun のインストールと bun init・bun add・npm との対照. This page follows bun.sh/docs for installation, projects, and the CLI. Bun is its own runtime and toolchain; trust bun --version and the live docs. For Python, see our uv vs pip/venv guide; for an earlier Bun intro, Bun install and TypeScript. To walk script output on a call, open wbroom; see create and join a room.
What Bun does in a repo
Bun (Oven) ships a JavaScript/TypeScript runtime, a package manager (npm/yarn/pnpm-like), and bundler/test tooling. Day to day you use bun init for package.json, bun add to install deps into the lockfile, bun run <script> for npm-style scripts, and bun run file.ts to execute sources without a separate compile step in many cases.
Install Bun
- 1
macOS / Linux script
Run the documented install command (often
curl -fsSL https://bun.sh/install | bash—confirm atbun.sh/docs/installation). Restart the shell or source the profile snippet the installer prints. - 2
Verify
bun --versionandwhich bunshould point at the official install. On Windows, follow PowerShell/WSL instructions on bun.sh. - 3
Alongside mise / nvm
If you use mise for Node, document whether this repo standardizes on Bun or Node so CI matches laptops.
bun init
- 1
Empty folder
bun initin the target directory; pick a template (blank, React, etc.—CLI options vary). You getpackage.jsonand oftentsconfig.json. - 2
Existing npm repo
With
package.jsonalready present, skip init and runbun install(Bun maintainsbun.lockper docs). - 3
First script
Add
"dev": "bun run src/index.ts"(or your framework default) sobun run devproves the toolchain.
bun add
- 1
Production deps
bun add lodashmirrorsnpm install lodash, updatingdependenciesand the lockfile. Dev deps:bun add -d typescript(flags per docs). - 2
Clone then install
After git clone,
bun install; in CI use frozen lock installs as documented. - 3
Don’t dual-lock blindly
Pick npm or Bun lock strategy; when migrating from npm, follow bun.sh guidance for
package-lock.jsonvsbun.lock.
bun run
- 1
package.json scripts
bun run devequalsnpm run dev. Some builtins omitrun(e.g.bun test)—checkbun --help. - 2
Run TypeScript directly
bun run src/index.ts(or forms in docs) often runs TS without a separate tsc step; production may still usebun buildor framework bundlers. - 3
Env and args
Pass env vars like npm (
PORT=3000 bun run dev) or use.envper Bun’s loading rules.
# First hour · Bun · 2026-10-20
curl -fsSL https://bun.sh/install | bash # confirm on bun.sh/docs
bun --version
mkdir my-app && cd my-app && bun init
bun add -d typescript
# package.json → "dev": "bun run src/index.ts"
bun run devBun vs npm / Node
| Goal | npm / Node | Bun |
|---|---|---|
| Global CLI | npm i -g pkg | bun add -g pkg (see docs) |
| Add dep | npm install pkg | bun add pkg |
| Install tree | npm ci / npm install | bun install |
| Run script | npm run dev | bun run dev |
| Run TS file | often ts-node / tsc first | often direct bun run file.ts |
Command not found
Reload shell; ensure ~/.bun/bin is on PATH; remove broken old installs.
Install failures
Some native modules need a specific Bun/Node version—run those paths on Node LTS if required.
CI drift
Commit bun.lock, pin Bun in CI, document bun install in README.
Suggested path: official install →
bun initorbun install→bun add→bun run dev. Trust bun.sh/docs and your localbun --help.
- Vs our other Bun post
- Earlier post focuses install + TypeScript; this one stresses init / add / run scripts vs npm.
- Vs uv
- uv is Python; Bun is JS/TS—see uv guide.
- Not a wbroom feature guide
- Bun CLI only; meetings on wbroom.
確認したい質問
Can Bun fully replace Node?
Many apps yes; some native addons or frameworks still require Node LTS—validate with your test suite and bun.sh compatibility notes.
Must I delete package-lock.json?
Standardize on one lockfile strategy; follow bun.sh migration docs instead of letting npm and Bun fight over locks.
bun add vs bun install?
bun add adds/upgrades named packages in the manifest; bun install installs the full tree from manifest + lock.
Do I need tests in hour one?
No—get bun run dev green first; add bun test when the template or team expects it.