TExcelTAD

Install Scripts & allowScripts

A postinstall script in any package of your tree runs arbitrary code on your machine the moment you type npm install, with your user's permissions, before you've imported a single line of it. A few packages need that (native addons, downloaders of platform binaries); most attacks on the npm ecosystem have used it too: a hijacked maintainer account publishes a patch release whose postinstall steals tokens from ~/.npmrc and environment variables. All three package managers now let you decide which dependencies may run install scripts.

Tool Default for dependencies' install scripts Where approvals live
npm 11.19 runs them, but warns about any not covered by allowScripts; --strict-allow-scripts makes it an error "allowScripts" in package.json
pnpm 10 doesn't run them; prints "Ignored build scripts" onlyBuiltDependencies in pnpm-workspace.yaml
Yarn 4 doesn't run them (enableScripts: false) enableScripts in .yarnrc.yml, dependenciesMeta.<pkg>.built in package.json

Your own project's scripts are never affected; this is only about scripts inside dependencies.

A package with a postinstall

Each session below first publishes a tiny package, hooky, whose postinstall appends a line to /tmp/hooky.log, so we can see whether it ran.

npm: warn, approve, deny

bash · try it yourself
mkdir -p hooky && cd hooky
echo '{ "name": "hooky", "version": "1.0.0", "scripts": { "postinstall": "echo hooky postinstall ran >> /tmp/hooky.log" } }' > package.json
npm publish > /dev/null 2>&1
mkdir ../app && cd ../app && npm init -y > /dev/null
npm install hooky 2>&1 | grep -v '^
  


cat /tmp/hooky.log
echo "--- review and deny"
npm install-scripts ls
npm install-scripts deny hooky > /dev/null
npm pkg get allowScripts
rm -rf node_modules /tmp/hooky.log
npm install > /dev/null 2>&1
cat /tmp/hooky.log 2>/dev/null || echo "(postinstall did not run)"
added 1 package in 411ms
npm warn install-scripts 1 package has install scripts not yet covered by allowScripts:
npm warn install-scripts   hooky@1.0.0 (postinstall: echo hooky postinstall ran >> /tmp/hooky.log)
npm warn install-scripts
npm warn install-scripts Run `npm install-scripts ls` to review, or `npm install-scripts approve <pkg>` to allow.
hooky postinstall ran
--- review and deny
1 package has install scripts not yet covered by allowScripts:
  hooky@1.0.0 (postinstall: echo hooky postinstall ran >> /tmp/hooky.log)

Run `npm install-scripts approve <pkg>` to allow, or `npm install-scripts deny <pkg>` to deny.
{
  "hooky": false
}
(postinstall did not run)

By default npm 11.19 still ran the script and warned. After npm install-scripts deny hooky, the decision is stored in package.json under allowScripts (false = deny, true = allow), so it is reviewed in pull requests like any other change, and the next install skipped the script. approve writes true; by default it pins the approval to the exact version ("esbuild@0.28.2": true), so a new release has to be approved again.

Make unreviewed scripts a hard error in CI:

bash · try it yourself
mkdir -p hooky && cd hooky
echo '{ "name": "hooky", "version": "1.0.0", "scripts": { "postinstall": "echo hooky postinstall ran >> /tmp/hooky.log" } }' > package.json
npm publish > /dev/null 2>&1
mkdir ../app && cd ../app && npm init -y > /dev/null
npm install hooky --strict-allow-scripts 2>&1 | grep -E 'code|--strict'
cat /tmp/hooky.log 2>/dev/null || echo "(postinstall did not run)"
npm error code ESTRICTALLOWSCRIPTS
npm error --strict-allow-scripts: 1 package(s) have install scripts not covered by allowScripts:
(postinstall did not run)

The blunt instrument is --ignore-scripts (or ignore-scripts=true in .npmrc): no install scripts at all, including your own project's prepare. It's safe but breaks packages that really need their script.

pnpm: off by default

bash · try it yourself
mkdir -p hooky && cd hooky
echo '{ "name": "hooky", "version": "1.0.0", "scripts": { "postinstall": "echo hooky postinstall ran >> /tmp/hooky.log" } }' > package.json
npm publish > /dev/null 2>&1
mkdir ../app && cd ../app && echo '{ "name": "app", "version": "1.0.0" }' > package.json
pnpm add hooky esbuild@0.28.2 2>&1 | grep -A3 Ignored
cat /tmp/hooky.log 2>/dev/null || echo "(postinstall did not run)"
echo "--- allow esbuild only"
printf 'onlyBuiltDependencies:\n  - esbuild\n' > pnpm-workspace.yaml
pnpm rebuild 2>&1 | grep postinstall
cat /tmp/hooky.log 2>/dev/null || echo "(hooky still did not run)"
│   Ignored build scripts: esbuild@0.28.2, hooky@1.0.0.                        │
│   Run "pnpm approve-builds" to pick which dependencies should be allowed     │
│   to run scripts.                                                            │
│                                                                              │
(postinstall did not run)
--- allow esbuild only
.../esbuild@0.28.2/node_modules/esbuild postinstall$ node install.js
.../esbuild@0.28.2/node_modules/esbuild postinstall: Done
(hooky still did not run)

pnpm approve-builds does the same interactively and writes the list for you. esbuild's postinstall only verifies its platform binary, so esbuild works even when it's ignored; packages that compile native code (node-gyp) do not.

Yarn: off unless you turn it on

bash · try it yourself
mkdir -p hooky && cd hooky
echo '{ "name": "hooky", "version": "1.0.0", "scripts": { "postinstall": "echo hooky postinstall ran >> /tmp/hooky.log" } }' > package.json
npm publish > /dev/null 2>&1
mkdir ../app && cd ../app
echo '{ "name": "app", "packageManager": "yarn@4.18.1" }' > package.json && touch yarn.lock
yarn config get enableScripts
yarn add hooky 2>&1 | grep YN0004
cat /tmp/hooky.log 2>/dev/null || echo "(postinstall did not run)"
false
➤ YN0004: │ hooky@npm:1.0.0 lists build scripts, but all build scripts have been disabled.
(postinstall did not run)

To allow scripts in Yarn, set enableScripts: true in .yarnrc.yml and switch individual packages off with "dependenciesMeta": { "hooky": { "built": false } } in package.json; there is no allow-list the other way round.

A policy that works

  • Default deny, allow by name. Keep the list short and review additions like code: esbuild, a native database driver, maybe sharp. Pin approvals to versions where the tool supports it.
  • Fail CI on anything new: npm ci --strict-allow-scripts, or rely on pnpm/Yarn's defaults.
  • Prefer packages that don't need scripts. Modern native tools ship prebuilt binaries as optional platform packages (esbuild, Rollup, SWC) instead of downloading in postinstall.
  • Keep secrets out of the install step: CI tokens for publishing belong to the publish job, not to the job that runs npm ci.

Test yourself

What does npm 11.19 do by default with a dependency's postinstall script that isn't covered by allowScripts?

  1. Skips it silently
  2. Runs it and prints a warning
  3. Fails the install
  4. Asks interactively

Where does pnpm 10 record which dependencies may run build scripts?

  1. allowScripts in package.json
  2. onlyBuiltDependencies in pnpm-workspace.yaml
  3. .npmrc ignore-scripts
  4. pnpm-lock.yaml

Why is --ignore-scripts a blunt instrument?

  1. It deletes node_modules
  2. It disables every install script, including your own project's prepare and packages that really need one
  3. It only works with Yarn
  4. It disables npm run