> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/oven-sh/bun/llms.txt
> Use this file to discover all available pages before exploring further.

# Adding & Removing Packages

> Add and remove dependencies with bun add and bun remove.

## Adding packages

Use `bun add` to add a package to your project. The package is installed and saved to `package.json`.

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

To add a specific version, version range, or dist-tag:

```bash theme={null}
bun add zod@3.20.0
bun add zod@^3.0.0
bun add zod@latest
bun add react@18
```

### Dev dependencies

Use `-d` (or `--dev`, `-D`, `--development`) to save to `devDependencies`:

```bash theme={null}
bun add -d @types/react
bun add --dev typescript
```

### Optional dependencies

Use `--optional` to save to `optionalDependencies`:

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

### Peer dependencies

Use `--peer` to save to `peerDependencies`:

```bash theme={null}
bun add --peer @types/bun
```

### Exact versions

Use `--exact` (or `-E`) to pin to the resolved version rather than a range:

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

This writes the resolved version without a caret:

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

Without `--exact`, Bun writes a caret range (`^18.2.0`), which allows compatible updates.

### Global packages

Use `-g` (or `--global`) to install a package globally without modifying the current project's `package.json`. This is typically used for CLI tools.

```bash theme={null}
bun add -g cowsay
cowsay "Bun!"
```

Configure the global installation directory in `bunfig.toml`:

```toml theme={null}
[install]
globalDir = "~/.bun/install/global"
globalBinDir = "~/.bun/bin"
```

## Git dependencies

Add a dependency from a public or private Git repository:

```bash theme={null}
bun add git@github.com:moment/moment.git
```

<Note>
  To install a private repository, your system needs the appropriate SSH credentials configured.
</Note>

Bun supports multiple Git protocols:

```json theme={null}
{
  "dependencies": {
    "dayjs": "git+https://github.com/iamkun/dayjs.git",
    "lodash": "git+ssh://github.com/lodash/lodash.git#4.17.21",
    "moment": "git@github.com:moment/moment.git",
    "zod": "github:colinhacks/zod"
  }
}
```

## Tarball dependencies

Add a package from a hosted `.tgz` file:

```bash theme={null}
bun add zod@https://registry.npmjs.org/zod/-/zod-3.21.4.tgz
```

This saves the tarball URL as the version in `package.json`:

```json theme={null}
{
  "dependencies": {
    "zod": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz"
  }
}
```

## Trusted dependencies

Bun does not run lifecycle scripts for installed dependencies by default. To allow scripts for a specific package, add it to `trustedDependencies` in `package.json`:

```json theme={null}
{
  "name": "my-app",
  "trustedDependencies": ["node-sass"]
}
```

See [Lifecycle Scripts](/pm/lifecycle) for more details on Bun's security model.

## Removing packages

Use `bun remove` to uninstall a package and remove it from `package.json`:

```bash theme={null}
bun remove ts-node
```

Multiple packages can be removed at once:

```bash theme={null}
bun remove eslint prettier @types/node
```

`bun remove` updates the lockfile and removes the package from `node_modules`.
