Documentation Index
Fetch the complete documentation index at: https://mintlify.com/zhcndoc/bun/llms.txt
Use this file to discover all available pages before exploring further.
Add a dependency to package.json and install it.
Behavior
bun add adds the specified package(s) to package.json and installs them. By default, packages are added to dependencies.
Arguments
Package name
Package with version
Package with tag
bun add react@latest
bun add react@next
bun add react@canary
Package with version range
bun add react@^18.0.0
bun add react@~18.2.0
Multiple packages
bun add react react-dom typescript
Scoped packages
bun add @types/react
bun add @babel/core
Git repositories
bun add git@github.com:user/repo.git
bun add github:user/repo
bun add user/repo # Short form
Git with branch/tag/commit
bun add user/repo#main
bun add user/repo#v1.0.0
bun add user/repo#abc123
Local packages (file:)
bun add file:../my-package
bun add file:./packages/utils
Tarball URLs
bun add https://example.com/package.tgz
Flags
--dev (-d, -D)
Add as a dev dependency.
bun add --dev typescript
bun add -D jest
Adds to devDependencies in package.json:
{
"devDependencies": {
"typescript": "^5.0.0"
}
}
--optional (-o, -O)
Add as an optional dependency.
bun add --optional fsevents
Adds to optionalDependencies in package.json:
{
"optionalDependencies": {
"fsevents": "^2.3.2"
}
}
--peer (-p, -P)
Add as a peer dependency.
Adds to peerDependencies in package.json:
{
"peerDependencies": {
"react": "^18.0.0"
}
}
--exact (-E)
Install exact version instead of version range.
Uses exact version in package.json:
{
"dependencies": {
"react": "18.2.0"
}
}
--global (-g)
Install package globally.
bun add --global typescript
--no-save
Install package without adding to package.json.
bun add --no-save webpack
--ignore-scripts
Skip running lifecycle scripts.
bun add --ignore-scripts puppeteer
--trust
Automatically trust packages with scripts.
--dry-run
Simulate adding packages without actually installing.
--cwd <path>
Run command in specified directory.
bun add --cwd ./my-project react
--backend <backend>
Specify installation backend (hardlink, clonefile, copyfile, symlink).
bun add --backend hardlink react
Examples
Add a production dependency
$ bun add react
bun add v1.0.0
installed react@18.2.0
1 package installed [245ms]
Add multiple dev dependencies
$ bun add -D typescript @types/node @types/react
bun add v1.0.0
installed typescript@5.3.3
installed @types/node@20.10.0
installed @types/react@18.2.45
3 packages installed [432ms]
Add with exact version
$ bun add --exact lodash
bun add v1.0.0
installed lodash@4.17.21
1 package installed [180ms]
Add from GitHub
$ bun add lodash/lodash#4.17.21
bun add v1.0.0
installed lodash@github:lodash/lodash#4.17.21
1 package installed [892ms]
Add local package
$ bun add file:../shared-components
bun add v1.0.0
installed shared-components@file:../shared-components
1 package installed [45ms]
Registry aliases
You can add packages with aliases:
bun add react-old@npm:react@16.14.0
This adds an aliased dependency:
{
"dependencies": {
"react-old": "npm:react@16.14.0"
}
}
Import using the alias:
import React from "react-old";