Skip to main content

npm lifecycle scripts: why to switch install scripts off and what breaks

When you run npm install, every package in the tree may run a preinstall, install or postinstall script on your machine, with your user's access to files, tokens and the network. Most packages do not need that. The ones that abuse it do not need anything else.

The large npm compromises of 2025 used exactly this hook, and the package managers have responded. npm 12 no longer runs dependency install scripts unless you approve them, and pnpm, Yarn and Bun all ship with a similar default. This post covers what changed, how to set an allowlist, and what breaks when scripts stop running.

Why install scripts matter

An install script runs before any of your code, often in CI with publish tokens in the environment and on developer laptops with SSH keys and cloud credentials on disk. Two 2025 incidents show the pattern:

  • The nx compromise (August 2025). The project's advisory says malicious versions "contained a postinstall script that scanned user's file system for text files", collected credentials and posted them to a new GitHub repository under the victim's account. It notes that installing through any client (npm, Yarn or pnpm) could trigger it.
  • Shai-Hulud (September 2025). GitHub describes a self-replicating worm that spread through compromised maintainer accounts by "injecting malicious post-install scripts into popular JavaScript packages", and says it removed more than 500 compromised packages. CISA's alert adds that it harvested GitHub tokens and cloud keys, then used the stolen npm access to publish infected versions of the victim's other packages.

The script is what turned one installed package into stolen credentials, and stolen credentials into the next wave.

What each package manager does now

Package manager Dependency install scripts by default How to allow one
npm 12 (12.0.0 published 8 July 2026) Blocked with a warning until approved npm approve-scripts <pkg>, written to allowScripts in package.json
npm 11 and earlier Run ignore-scripts=true to turn all off
pnpm (current) Not run unless allowed allowBuilds
Yarn (current docs) Not run for third-party packages; check your version, older releases ran them enableScripts with dependenciesMeta
Bun Not run, except a curated default list trustedDependencies in package.json

Sources: GitHub's npm 12 changelog, the npm 12 config reference, pnpm's supply chain page, Yarn's configuration reference and Bun's lifecycle docs.

In npm 12 the blocked set covers preinstall, install and postinstall from dependencies, a package with a binding.gyp and no explicit script, and prepare scripts from git, file and link dependencies. The same release stops resolving git dependencies and remote tarball URLs unless you opt in with --allow-git and --allow-remote. If you are still on npm 11.16 or later, the policy is available as warnings today.

Pinning to an old npm or switching everything back on with dangerously-allow-all-scripts undoes the protection. The npm docs call that flag "a migration escape hatch only".

Setting up an allowlist with npm

Start by seeing what would be blocked:

npm approve-scripts --allow-scripts-pending

For each package listed, ask whether it really needs to run code at install time, then approve or deny it:

npm approve-scripts better-sqlite3 esbuild    # pinned to the installed version by default
npm deny-scripts some-telemetry-package

The allowlist is written to package.json and belongs in version control, so a reviewer sees a new entry in a pull request. Approvals are pinned to the version you reviewed unless you pass --no-allow-scripts-pin, which is the right default: a hijacked release of an approved package gets a new version number and needs a new approval. In CI, set strict-allow-scripts=true so an unreviewed script fails the install instead of producing a warning nobody reads.

On older npm versions, the blunt tool still works:

# .npmrc
ignore-scripts=true

Then run the one package that needs its build explicitly, after reviewing it, with npm rebuild <name> --ignore-scripts=false.

What breaks

Most of the tree keeps working, because most packages ship plain JavaScript or prebuilt binaries. The packages that notice are predictable:

  • Native addons compiled at install time with node-gyp. Without the build step, the first require fails because the .node file was never built.
  • Packages that download a binary at install time, such as a headless browser or a desktop runtime. Install succeeds; the first run fails because the binary is missing.
  • Packages that only print a message or send telemetry in postinstall. Nothing breaks. These are the ones to deny.
  • Your own project's hooks. ignore-scripts=true also skips your root prepare script, so tools that set up git hooks on install stop doing so. npm 12's policy targets dependencies; check how your own scripts behave on the version you run.

Two habits make the failures easy to diagnose. Install once with the policy on and run your test suite, so missing builds show up in CI rather than production. And when a package fails at runtime, check whether it has an install script before you blame the code.

Install scripts in CI

CI is where install scripts do the most damage, because CI is where publish tokens and deploy credentials live. In CI:

  1. Use npm ci, which installs exactly what the lockfile says.
  2. Keep the install in a job that holds no publish or deploy secrets.
  3. Turn scripts off, or run the strict allowlist, so an unreviewed script fails the build.
  4. Publish from a separate job with trusted publishing rather than a stored token.

Lifecycle scripts are one route among several. Turning them off does not help if your code requires the compromised package and runs it in production. It removes the route that fires before anyone looks.

What Vulkro checks

Vulkro Core reads package.json, .npmrc and workflow files in a normal scan:

Rule What it flags
npm-install-lifecycle-script A package.json that declares an install-time hook
supply-ci-install-script An install hook that runs a risky command: a remote fetch, eval or a shell
NPMRC-SCRIPTS-001 A package-manager config that turns install scripts back on globally (ignore-scripts=false, dangerously-allow-all-builds=true, allow-git=all)
install-agent-cli An install hook that invokes an AI agent CLI
SUP-COMPROMISE-003 A dependency version that matches a known malicious lifecycle payload
vulkro scan .                 # the rules above, plus known-compromised versions
vulkro inspect ./new-dep      # malicious-capability shapes in source you have not run yet

vulkro inspect is useful before you approve a script: point it at the unpacked package and read what it surfaces. It never declares a package safe. It shows you what to read.

FAQ

Does npm ignore-scripts break my own build scripts?

ignore-scripts=true stops npm running scripts from package.json files, including your project's own lifecycle hooks such as prepare. Commands you run by name, such as npm test or npm run build, still run their script, but their pre and post scripts do not.

Which packages actually need install scripts?

Mainly native addons that compile with node-gyp and packages that download a platform binary at install time. Most other packages run fine without them. Review each one the package manager reports and allow only those whose script does real build work.

Is turning off install scripts enough to stop npm supply chain attacks?

No. It blocks the attacks that run at install time, which were the fastest-spreading ones in 2025. A malicious package can still run when your code imports it, so pair it with lockfiles, pinned versions, a short delay before adopting new releases and checks for known-compromised versions.

See the supply chain use case and the malicious install hooks rule, or start a 14-day trial. Related: the npm worms of 2026 and what the Shai-Hulud worm changed for CI.