npm
can define lifecycle scripts in their package.json
. Some of the most common are below, but there are many others.
preinstall
: Runs before the package is installedpostinstall
: Runs after the package is installedpreuninstall
: Runs before the package is uninstalledprepublishOnly
: Runs before the package is published
npm
clients—Bun does not execute arbitrary lifecycle scripts by default.
postinstall
The postinstall
script is particularly important. It’s widely used to build or install platform-specific binaries for packages that are implemented as native Node.js add-ons. For example, node-sass
is a popular package that uses postinstall
to build a native binary for Sass.
package.json
trustedDependencies
Instead of executing arbitrary scripts, Bun uses a “default-secure” approach. You can add certain packages to an allow list, and Bun will execute lifecycle scripts for those packages. To tell Bun to allow lifecycle scripts for a particular package, add the package name to trustedDependencies
array in your package.json
.
package.json
trustedDependencies
, install/re-install the package. Bun will read this field and run lifecycle scripts for my-trusted-package
.
As of Bun v1.0.16, the top 500 npm packages with lifecycle scripts are allowed by default. You can see the full list here.
--ignore-scripts
To disable lifecycle scripts for all packages, use the --ignore-scripts
flag.
terminal