Skip to main content

Adding packages

Use bun add to add a package to your project. The package is installed and saved to package.json.
bun add preact
To add a specific version, version range, or dist-tag:
bun add [email protected]
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:
bun add -d @types/react
bun add --dev typescript

Optional dependencies

Use --optional to save to optionalDependencies:
bun add --optional lodash

Peer dependencies

Use --peer to save to peerDependencies:
bun add --peer @types/bun

Exact versions

Use --exact (or -E) to pin to the resolved version rather than a range:
bun add react --exact
This writes the resolved version without a caret:
{
  "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.
bun add -g cowsay
cowsay "Bun!"
Configure the global installation directory in bunfig.toml:
[install]
globalDir = "~/.bun/install/global"
globalBinDir = "~/.bun/bin"

Git dependencies

Add a dependency from a public or private Git repository:
bun add [email protected]:moment/moment.git
To install a private repository, your system needs the appropriate SSH credentials configured.
Bun supports multiple Git protocols:
{
  "dependencies": {
    "dayjs": "git+https://github.com/iamkun/dayjs.git",
    "lodash": "git+ssh://github.com/lodash/lodash.git#4.17.21",
    "moment": "[email protected]:moment/moment.git",
    "zod": "github:colinhacks/zod"
  }
}

Tarball dependencies

Add a package from a hosted .tgz file:
bun add zod@https://registry.npmjs.org/zod/-/zod-3.21.4.tgz
This saves the tarball URL as the version in package.json:
{
  "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:
{
  "name": "my-app",
  "trustedDependencies": ["node-sass"]
}
See Lifecycle Scripts for more details on Bun’s security model.

Removing packages

Use bun remove to uninstall a package and remove it from package.json:
bun remove ts-node
Multiple packages can be removed at once:
bun remove eslint prettier @types/node
bun remove updates the lockfile and removes the package from node_modules.

Build docs developers (and LLMs) love