> ## 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.

# bun add

Add a dependency to `package.json` and install it.

```bash theme={null}
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

```bash theme={null}
bun add react
```

### Package with version

```bash theme={null}
bun add react@18.2.0
```

### Package with tag

```bash theme={null}
bun add react@latest
bun add react@next
bun add react@canary
```

### Package with version range

```bash theme={null}
bun add react@^18.0.0
bun add react@~18.2.0
```

### Multiple packages

```bash theme={null}
bun add react react-dom typescript
```

### Scoped packages

```bash theme={null}
bun add @types/react
bun add @babel/core
```

### Git repositories

```bash theme={null}
bun add git@github.com:user/repo.git
bun add github:user/repo
bun add user/repo  # Short form
```

### Git with branch/tag/commit

```bash theme={null}
bun add user/repo#main
bun add user/repo#v1.0.0
bun add user/repo#abc123
```

### Local packages (file:)

```bash theme={null}
bun add file:../my-package
bun add file:./packages/utils
```

### Tarball URLs

```bash theme={null}
bun add https://example.com/package.tgz
```

## Flags

### `--dev` (`-d`, `-D`)

Add as a dev dependency.

```bash theme={null}
bun add --dev typescript
bun add -D jest
```

Adds to `devDependencies` in `package.json`:

```json theme={null}
{
  "devDependencies": {
    "typescript": "^5.0.0"
  }
}
```

### `--optional` (`-o`, `-O`)

Add as an optional dependency.

```bash theme={null}
bun add --optional fsevents
```

Adds to `optionalDependencies` in `package.json`:

```json theme={null}
{
  "optionalDependencies": {
    "fsevents": "^2.3.2"
  }
}
```

### `--peer` (`-p`, `-P`)

Add as a peer dependency.

```bash theme={null}
bun add --peer react
```

Adds to `peerDependencies` in `package.json`:

```json theme={null}
{
  "peerDependencies": {
    "react": "^18.0.0"
  }
}
```

### `--exact` (`-E`)

Install exact version instead of version range.

```bash theme={null}
bun add --exact react
```

Uses exact version in `package.json`:

```json theme={null}
{
  "dependencies": {
    "react": "18.2.0"
  }
}
```

### `--global` (`-g`)

Install package globally.

```bash theme={null}
bun add --global typescript
```

### `--no-save`

Install package without adding to `package.json`.

```bash theme={null}
bun add --no-save webpack
```

### `--ignore-scripts`

Skip running lifecycle scripts.

```bash theme={null}
bun add --ignore-scripts puppeteer
```

### `--trust`

Automatically trust packages with scripts.

```bash theme={null}
bun add --trust esbuild
```

### `--dry-run`

Simulate adding packages without actually installing.

```bash theme={null}
bun add --dry-run react
```

### `--cwd <path>`

Run command in specified directory.

```bash theme={null}
bun add --cwd ./my-project react
```

### `--backend <backend>`

Specify installation backend (hardlink, clonefile, copyfile, symlink).

```bash theme={null}
bun add --backend hardlink react
```

## Examples

### Add a production dependency

```bash theme={null}
$ bun add react
bun add v1.0.0

 installed react@18.2.0

 1 package installed [245ms]
```

### Add multiple dev dependencies

```bash theme={null}
$ 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

```bash theme={null}
$ bun add --exact lodash
bun add v1.0.0

 installed lodash@4.17.21

 1 package installed [180ms]
```

### Add from GitHub

```bash theme={null}
$ 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

```bash theme={null}
$ 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:

```bash theme={null}
bun add react-old@npm:react@16.14.0
```

This adds an aliased dependency:

```json theme={null}
{
  "dependencies": {
    "react-old": "npm:react@16.14.0"
  }
}
```

Import using the alias:

```ts theme={null}
import React from "react-old";
```
