This page documents the command-line interface for Bun's package management commands: bun install, bun add, bun remove, bun update, and bun link. These commands provide the user-facing interface to Bun's package manager functionality.
For information about the underlying package manager implementation, dependency resolution, and lockfile format, see Package Manager. For details on CLI architecture and command routing, see CLI Architecture and Command Dispatch.
Bun provides the following package management and initialization commands:
| Command | Purpose | Aliases |
|---|---|---|
bun install | Install all dependencies from package.json | bun i |
bun add <packages...> | Add dependencies to package.json | bun a |
bun remove <packages...> | Remove dependencies from package.json | bun rm |
bun update <packages...> | Update dependencies to latest versions | bun up |
bun link | Register or link local packages | - |
bun unlink | Unregister linked packages | - |
bunx <package> | Execute package binaries (ephemeral install) | bun x |
bun create <template> [dest] | Scaffold new projects from templates | - |
bun init | Initialize a new project interactively | - |
All commands support common options like --registry, --cwd, and environment-specific flags.
Sources: test/cli/install/bun-install.test.ts1-850 test/cli/install/bun-add.test.ts1-900 test/cli/install/bun-remove.test.ts1-200 test/cli/install/bun-update.test.ts1-250 test/cli/install/bun-link.test.ts1-300 src/cli/bunx_command.zig1-128 src/cli/create_command.zig1-190 src/cli/init_command.zig1-143
Sources: src/install/lockfile.zig218-370 src/install/migration.zig1-200 test/cli/install/bun-install.test.ts73-245
The bun install command reads package.json, resolves dependencies, downloads packages from registries, and installs them into node_modules.
Sources: src/install/lockfile.zig218-400 src/install/extract_tarball.zig108-200 src/install/migration.zig3-100
| Option | Type | Description | Default |
|---|---|---|---|
--registry | URL | Override default npm registry | https://registry.npmjs.org |
--network-concurrency | Number (0-65535) | Limit concurrent network requests | System default |
--cwd | Path | Working directory for install | Current directory |
--frozen-lockfile | Boolean | Fail if lockfile needs update | false |
--production | Boolean | Install only production dependencies | false |
--dry-run | Boolean | Simulate install without changes | false |
--force | Boolean | Force reinstall all packages | false |
--cache | Boolean | Enable/disable cache | true |
The --network-concurrency option must be a number between 0 and 65535. Values outside this range will cause an error:
Expected --network-concurrency to be a number between 0 and 65535
Sources: test/cli/install/bun-install.test.ts74-104 test/cli/install/bun-install.test.ts400-460
When no bun.lock or bun.lockb exists, Bun automatically detects and migrates from other package managers:
Sources: src/install/migration.zig3-100 test/cli/install/migration/migrate.test.ts10-50
Common errors during bun install:
ConnectionRefused or ConnectionClosedSources: test/cli/install/bun-install.test.ts341-398 test/cli/install/bun-install.test.ts612-646
The bun add command adds one or more dependencies to package.json and installs them.
Sources: test/cli/install/bun-add.test.ts43-93
bun add supports various path formats:
All of these result in a file: dependency in package.json:
Sources: test/cli/install/bun-add.test.ts276-324
The --only-missing flag skips installation if the package is already installed:
Sources: test/cli/install/bun-add.test.ts128-200
The --analyze flag scans source files to detect dependencies:
This analyzes the import graph starting from the specified file and adds any missing npm packages to package.json.
Sources: test/cli/install/bun-add.test.ts202-274
Sources: src/install/dependency.zig282-800 test/cli/install/bun-add.test.ts1-900
The bun remove command removes dependencies from package.json and the installed packages from node_modules.
Sources: test/cli/install/bun-remove.test.ts22-90 src/install/lockfile.zig555-650
When removing packages, Bun calls Lockfile.clean() to remove unused dependencies from the lockfile. This process:
preinstall_state arraySources: src/install/lockfile.zig555-773
The bun update command updates dependencies to their latest compatible versions according to semver ranges in package.json.
Sources: test/cli/install/bun-update.test.ts30-150 src/install/lockfile.zig467-554
The update logic in Lockfile.preprocessUpdateRequests() determines new version strings:
--exact flag: Use exact version "1.2.3""^1.2.3"Dependency.version fields in lockfileSources: src/install/lockfile.zig467-554
The bun link command creates symlinks between local packages for development.
Sources: test/cli/install/bun-link.test.ts36-200
For workspace packages, bun link can link packages within a monorepo:
Workspace packages are identified by Resolution.Tag.workspace in the lockfile.
Sources: test/cli/install/bun-link.test.ts36-100 src/install/resolution.zig42-80
The bunx command (alias: bun x) executes package binaries without permanently installing them. This is similar to npx but uses Bun's package manager for faster execution.
Diagram: bunx execution pipeline
Sources: src/cli/bunx_command.zig1-128 src/cli/bunx_command.zig32-98
| Option | Type | Description | Default |
|---|---|---|---|
--package / -p | String | Package to install (when binary name differs) | Inferred from command |
--bun / -b | Boolean | Run using bun runtime | false |
--silent | Boolean | Suppress installation output | false |
--verbose | Boolean | Show detailed installation logs | false |
--no-install | Boolean | Error if package not cached | false |
The --package flag allows running a binary from a package with a different name:
Sources: src/cli/bunx_command.zig32-98
Diagram: bunx cache structure
The temporary installation directory format is /tmp/bunx-{uid}-{pkg} on Unix systems, where {uid} is the user ID and {pkg} is a hash of the package name and version.
Sources: src/cli/bunx_command.zig128-250 test/cli/install/bunx.test.ts1-200
When --package is not specified, bunx infers the package name from the binary name:
The binary resolution follows this order:
bin field in package.jsonnode_modules/.bin/Sources: src/cli/bunx_command.zig99-127 src/cli/run_command.zig387-415
Common bunx errors:
Package not found with --no-install:
error: Package 'some-package' not found in cache
Binary not found in package:
error: Binary 'some-bin' not found in package 'some-package'
Invalid package name:
error: --package requires a non-empty package name
Sources: src/cli/bunx_command.zig62-90 test/cli/install/bunx.test.ts100-150
The bun create command scaffolds new projects from templates. Templates can be official Bun examples, npm packages, GitHub repositories, or local directories.
Diagram: bun create project scaffolding pipeline
Sources: src/cli/create_command.zig142-231 src/cli/create_command.zig1-52
| Option | Type | Description | Default |
|---|---|---|---|
--force | Boolean | Overwrite existing files | false |
--no-install | Boolean | Skip bun install | false |
--no-git | Boolean | Skip git init | false |
--no-package-json | Boolean | Skip package.json transforms | false |
--verbose | Boolean | Show detailed logs | false |
--open | Boolean | Open in browser after creation | false |
Sources: src/cli/create_command.zig142-189
The create command skips certain directories and files to avoid copying unnecessary data:
Skipped directories (defined in skip_dirs):
node_modules.gitSkipped files (defined in skip_files):
package-lock.jsonyarn.lockpnpm-lock.yamlNever conflict files (defined in never_conflict):
README.md.gitignore.git/These files are always copied, even if they exist at the destination.
Sources: src/cli/create_command.zig12-28
Unless --no-package-json is specified, bun create modifies the template's package.json:
Diagram: package.json transformation pipeline
The script replacement logic (defined in replacePackageManagerRun):
npm run → bun runyarn run → bun runpnpm run → bun runnpx → bun xpnpm dlx → bun xSources: src/cli/create_command.zig31-51 src/cli/run_command.zig86-204
Bun provides official example templates in the examples/ directory. These can be accessed using shorthand names:
The template resolution checks for official examples first before attempting to download from npm or git.
Sources: src/cli/create_command.zig194-231 test/cli/install/bun-create.test.ts1-100
By default, bun create initializes a git repository in the created project:
git init in project directory.gitignore from templateThis can be skipped with --no-git.
Sources: src/cli/create_command.zig142-189
The bun init command initializes a new Bun project interactively, creating package.json, tsconfig.json, and optionally a .gitignore.
Diagram: bun init interactive setup
Sources: src/cli/init_command.zig1-143 src/cli/init_command.zig2-40
The prompt() function reads user input with default values:
package name: (my-project)
Users can press Enter to accept defaults or type new values. The prompt system:
Sources: src/cli/init_command.zig2-40
Diagram: Package type radio button menu
The menu uses ANSI escape codes for visual feedback:
❯ indicates current selectionSources: src/cli/init_command.zig43-143
bun init creates the following files:
For executable type, adds:
node_modules
dist
*.log
.DS_Store
Sources: src/cli/init_command.zig1-300 test/cli/init/init.test.ts6-150
With the -y flag, bun init uses default values without prompts:
Defaults:
"0.0.1""module""index.ts"Sources: test/cli/init/init.test.ts6-30
Options shared across multiple package commands:
The registry configuration supports:
Sources: test/cli/install/bun-install.test.ts400-460 src/install/npm.zig196-378
The --network-concurrency option limits parallel HTTP requests to prevent overwhelming servers or hitting rate limits. The implementation tracks concurrent requests and enforces the limit in the download pipeline.
Sources: test/cli/install/bun-install.test.ts106-206
Sources: test/harness.ts37-63
Commands validate package specifiers before processing:
Sources: src/install/dependency.zig153-280 test/cli/install/bun-add.test.ts95-126
Network errors are reported with context:
error: ConnectionRefused downloading package manifest bar
error: GET http://localhost:PORT/bar - 404
Sources: test/cli/install/bun-install.test.ts341-398
If tarball checksums don't match:
error: Integrity check failed for tarball: package-name
The integrity is verified in ExtractTarball.run() before extraction.
Sources: src/install/extract_tarball.zig13-27
Sources: src/install/install.zig1-291 src/install/lockfile.zig1-800 src/install/dependency.zig1-900 src/install/npm.zig1-400 src/install/extract_tarball.zig1-300 src/install/migration.zig1-200 test/cli/install/bun-install.test.ts1-850 test/cli/install/bun-add.test.ts1-900 test/cli/install/bun-remove.test.ts1-200 test/cli/install/bun-update.test.ts1-250 test/cli/install/bun-link.test.ts1-300
Refresh this wiki