Available scripts
Installation scripts
Run duringbun install:
preinstall- Before package is installedinstall- After package is installedpostinstall- After package and its dependencies are installedprepare- After package is installed and before publishingpreprepare- Beforepreparepostprepare- Afterprepare
Publishing scripts
Run duringbun publish:
prepublishOnly- Before package is publishedprepack- Before tarball is createdpostpack- After tarball is createdpublish- After package is publishedpostpublish- After package is published
Other scripts
Run during specific commands:preuninstall- Before package is removeduninstall- When package is removedpostuninstall- After package is removed
Defining scripts
Define lifecycle scripts inpackage.json:
Execution order
During bun install
For each package:
preinstall- Install package files
install/postinstall(of dependencies)postinstall(of current package)preprepare/prepare/postprepare
During bun publish
prepareprepublishOnlyprepack- Create tarball
postpack- Upload to registry
publishpostpublish
Common use cases
Build native modules
Build TypeScript
Download binaries
Generate files
Setup database
Disabling scripts
Skip all lifecycle scripts
Via bunfig.toml
Skip specific packages
Bun skips scripts for untrusted packages by default.Trusted dependencies
By default, Bun only runs scripts for trusted packages.Default trusted packages
Bun trusts these packages by default:esbuildpuppeteersharpmsgpackr-extract@biomejs/biome- Many more (see
bun pm default-trusted)
View default trusted list
Trust specific packages
Add totrustedDependencies in package.json:
Trust during install
View untrusted dependencies
Trust untrusted dependencies
Script environment
Environment variables
Bun sets these variables:npm_package_name- Package namenpm_package_version- Package versionnpm_lifecycle_event- Current script namenpm_lifecycle_script- Current script commandINIT_CWD- Original working directory
Example
Parallel execution
Bun runs scripts in parallel when possible.Set concurrency
Via bunfig.toml
Exit codes
Success
Script exits with code0:
Failure
Script exits with non-zero code:Ignore failures
Prefix command with- to ignore failures:
Shell
Bun uses the system shell to run scripts:- Unix:
/bin/sh - Windows:
cmd.exe
Shell commands
Node scripts
Bun scripts
Workspace scripts
In monorepos, lifecycle scripts run for each workspace package.Root scripts
Run after all workspace packages:Workspace package scripts
Run for each workspace:Best practices
Keep scripts fast
Slow scripts delay installations:Use prepare for publishing
Build before publishing:
bun publish and after bun install.
Don’t use postinstall for building
Avoid building in postinstall for packages: