Skip to main content
Add a dependency to package.json and install it.
bun add <package>

Behavior

bun add adds the specified package(s) to package.json and installs them. By default, packages are added to dependencies.

Arguments

Package name

bun add react

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 [email protected]: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.
bun add --peer react
Adds to peerDependencies in package.json:
{
  "peerDependencies": {
    "react": "^18.0.0"
  }
}

--exact (-E)

Install exact version instead of version range.
bun add --exact react
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.
bun add --trust esbuild

--dry-run

Simulate adding packages without actually installing.
bun add --dry-run react

--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 [email protected]

 1 package installed [245ms]

Add multiple dev dependencies

$ bun add -D typescript @types/node @types/react
bun add v1.0.0

 installed [email protected]
 installed @types/[email protected]  
 installed @types/[email protected]

 3 packages installed [432ms]

Add with exact version

$ bun add --exact lodash
bun add v1.0.0

 installed [email protected]

 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:[email protected]
This adds an aliased dependency:
{
  "dependencies": {
    "react-old": "npm:[email protected]"
  }
}
Import using the alias:
import React from "react-old";

Build docs developers (and LLMs) love