LinuxCommandLibrary

bun-add

Add dependencies to a Bun project

TLDR

Install a single package

$ bun add [package]
copy

Install multiple packages
$ bun add [package1 package2 ...]
copy

Install from a Git repository
$ bun add [git_url]
copy

Install a specific version
$ bun add [package]@[version]
copy

Install from local file or directory
$ bun add file:[path/to/file_or_directory]
copy

Add a dev dependency
$ bun add [[-d|--dev]] [package]
copy

Add a package globally
$ bun add [[-g|--global]] [package]
copy

SYNOPSIS

bun add [flags] [packages...]

PARAMETERS

--dev, -d
    Adds the specified packages to the devDependencies section of package.json.

--exact
    Installs and saves the package with an exact version number, preventing future automatic updates to minor or patch versions (e.g., lodash@1.2.3 instead of ^1.2.3).

--filter <pattern>
    In a monorepo setup, adds packages to specific workspaces whose names match the provided pattern.

--global, -g
    Installs the package globally on the system, making its executables available in the system's PATH. Global packages are not tied to a specific project's dependencies.

--no-save
    Installs the package but does not add it to the dependencies (or any other dependency type) section in package.json.

--optional
    Adds the specified packages to the optionalDependencies section of package.json. These packages are not critical for the application's functionality and may fail to install without stopping the entire process.

--peer
    Adds the specified packages to the peerDependencies section of package.json. Peer dependencies are typically required by a plugin or library to specify compatible versions of dependencies that their host project should already have.

--workspace
    When run within a workspace in a monorepo, this flag adds the package to the root workspace's package.json instead of the current sub-workspace.

DESCRIPTION

bun add is a subcommand of the Bun JavaScript runtime and package manager. It is used to install and add new packages as dependencies to a JavaScript or TypeScript project, similar to how npm install or yarn add function. When executed, it downloads the specified packages, updates the project's package.json file with the new dependency entries, and updates the bun.lockb lockfile to ensure reproducible installations. It supports adding various types of dependencies, including regular dependencies, development dependencies, optional dependencies, and peer dependencies, by using specific flags. bun add is a core part of Bun's integrated toolchain, offering a fast and efficient way to manage project dependencies.

CAVEATS

bun add is specifically designed for JavaScript/TypeScript project dependency management and requires the Bun runtime to be installed on your system. It is not a system package manager (like apt, yum, or dnf) and does not manage system-level software. The lockfile (bun.lockb) is Bun-specific and not compatible with other package managers like npm or Yarn's lockfiles.

VERSION SPECIFIERS

Packages can be added with specific version requirements, such as bun add lodash@^4.17.21 (caret range), bun add express@~4.0.0 (tilde range), bun add react@18.2.0 (exact version), or bun add webpack@latest (latest available). You can also specify packages from local paths (file:../my-package) or Git repositories.

MULTIPLE PACKAGES

You can add multiple packages in a single command, for example: bun add react react-dom axios. All specified packages will be added according to the flags provided.

HISTORY

Bun is a relatively new JavaScript runtime and toolkit, created by Jarred Sumner, with its initial public release in 2022. It was developed from the ground up to offer significantly faster performance than existing JavaScript runtimes and package managers. bun add is an integral part of Bun's integrated package manager, aiming to provide a high-performance alternative to traditional tools like npm and Yarn for managing project dependencies. Its development focuses on speed, simplicity, and an all-in-one developer experience.

SEE ALSO

bun install, bun remove, bun upgrade, npm install(1), yarn add(1)

Copied to clipboard