网页 综合 更新时间 2026-11-16 08:41:22

bun install | Bun Docs

自动蜘蛛 bun.sh 正文约 6000 字
bun install | Bun Docs
Publishing & Analysis
Workspace Management
Advanced Configuration
Install packages with Bun's fast package manager
The bun CLI contains a Node.js-compatible package manager designed to be a dramatically faster replacement for npm, yarn, and pnpm. It's a standalone tool that works in existing Node.js projects; if your project has a package.json, you can use bun install.
⚡️ 25x faster — Switch from npm install to bun install in any Node.js project to make your installations up to 25x faster.
To install all dependencies of a project:
To modify logging verbosity:
Unlike other npm clients, Bun does not execute arbitrary lifecycle scripts like postinstall for installed dependencies. Executing arbitrary scripts represents a potential security risk.
To tell Bun to allow lifecycle scripts for a particular package, add the package to trustedDependencies in your package.json.
Then re-install the package. Bun reads this field and runs lifecycle scripts for my-trusted-package.
Lifecycle scripts run in parallel during installation. To adjust the maximum number of concurrent scripts, use the --concurrent-scripts flag. The default is two times the reported cpu count or GOMAXPROCS.
Bun automatically optimizes postinstall scripts for popular packages (like esbuild and sharp) by determining which scripts need to run. To disable these optimizations:
Bun supports "workspaces" in package.json. See workspaces.
In a monorepo, you can install the dependencies for a subset of packages using the --filter flag.
Bun supports npm's "overrides" and Yarn's "resolutions" in package.json. Both specify a version range for metadependencies, the dependencies of your dependencies. See overrides and resolutions.
To add a dependency or peer dependency that a third-party package forgot to declare, use pnpm's and Yarn's "packageExtensions". See package extensions.
To install a package globally, use the -g/--global flag. Use it to install command-line tools.
To install in production mode (without devDependencies):
--production implies --frozen-lockfile. It only controls what gets installed. devDependencies already in node_modules from an earlier install stay there. Use bun prune --production to remove them.
For reproducible installs, use --frozen-lockfile. Bun installs the exact versions specified in the lockfile and does not update it. If your package.json disagrees with bun.lock, Bun exits with an error.
package.json disagrees with bun.lock when an edit adds a package, removes one, or changes a version. The error names the first dependency that changed. Run bun install and commit the updated bun.lock to fix it.
Edits that keep every package at its locked version don't fail the install. For example, you can change ^1.0.0 to >=1.0.0, move a dependency to another group, or add a dependency that another workspace already has. The same goes for a workspace's version, bin and scripts, and for trustedDependencies and patchedDependencies. If the package.json of a file: directory changed its dependencies, Bun prints a warning and installs what's locked.
Bun does not enable --frozen-lockfile automatically in CI; pass the flag or use bun ci. If there is no lockfile at all, --frozen-lockfile installs from package.json without writing one.
If the project has a package-lock.json, yarn.lock, or pnpm-lock.yaml but no bun.lock, --frozen-lockfile migrates that lockfile in memory and installs from it. The install fails if the migrated lockfile does not match package.json. Bun writes nothing in this case: no bun.lock, and none of the package.json edits a pnpm migration or a Yarn migration makes. It prints a note: instead. To finish the migration, run bun install without the flag and commit the files it writes. The bun.lockb to bun.lock conversion described in lockfile is the one write --frozen-lockfile still performs.
--frozen-lockfile works on a pruned monorepo checkout (e.g. turbo prune output, or a Docker context with only some workspace folders copied in). If a workspace listed in bun.lock is missing its package.json on disk, Bun skips it with a note: and does not install its exclusive dependencies. If a remaining workspace depends on a skipped one, the install fails. A registry package whose peer or optional dependency bun.lock links to a skipped workspace is installed without that link.
To validate the lockfile without installing, use bun install --frozen-lockfile --dry-run. The command exits with an error if package.json disagrees with bun.lock, and it does not run lifecycle scripts.
With a warm cache, bun install can run without the network:
--prefer-offline is also what install.prefer = "offline" in bunfig.toml selects; install.offline = true is the config form of --offline. With a complete restored cache, --offline --frozen-lockfile makes a CI install fully deterministic and network-free; --prefer-offline still fetches whatever the cache is missing.
An install is only certain to cache the packages that it places in node_modules. To warm the cache for an --offline install, run an install on the same platform that places at least the same packages. An install with --production or --omit does not cache the groups it leaves out.
--offline skips an optional dependency that is not cached, and anything below it that is not cached either. A package that an installed package requires is still an error.
See lockfile for more on bun.lock.
To omit dev, peer, or optional dependencies, use the --omit flag.
A package listed under both devDependencies and optional peerDependencies is left out by --omit dev and --production, unless an installed package still depends on it.
To perform a dry run, without installing anything:
Bun resolves the dependency tree and lists every package in it, installed already or not. During a dry run, Bun does not:
To write yarn.lock, run bun install --yarn without --dry-run.
Bun supports installing dependencies from Git, GitHub, and local or remotely-hosted tarballs. See bun add.
Bun supports two package installation strategi
访问原文 返回搜索 https://bun.sh/docs/pm/cli/install
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。