skip to content

In a project on npm 12, how does `--ignore-scripts` differ from the `allowScripts` policy, and which scripts still run when it is set?

level: seniorimportance: nice to knowfreq 7%

answer

  1. two switches with different reach
  2. dependencies only versus every package.json
  3. the root project's own lifecycle
  4. named scripts run, their hooks do not
  5. which setting wins over the others

basics

~20 s

allowScripts only governs dependencies' install scripts and leaves the root project's scripts running; --ignore-scripts stops every lifecycle script, root included, and every pre and post hook. A script you name with npm run, npm test, npm start and similar commands still runs.

solid answer

~40 s

In npm 12 the default-deny `allowScripts` policy covers **dependencies'** `preinstall`, `install`, `postinstall` (and `prepare` for non-registry sources), package by package; the root project's own lifecycle scripts still run. `--ignore-scripts` is a blanket switch: npm runs no scripts from any `package.json` during installs — not the dependencies', and not the root's `preinstall`, `postinstall` or `prepare`. Commands that exist to run one script — `npm run`, `npm test`, `npm start`, `npm stop`, `npm restart` — still run that script, but skip its `pre` and `post` hooks. It also outranks the policy flags: `--ignore-scripts` wins over `allowScripts`, `strict-allow-scripts` and `--dangerously-allow-all-scripts`. So a CI install with it set quietly skips the Git-hook `prepare`, approved native builds and every `prebuild` step; `npm rebuild <pkg>`, run without the setting, brings selected install scripts back.

go deeper

for a junior

Recall that ignore-scripts turns off lifecycle scripts everywhere, while npm run, npm test and npm start still run the script you name, just without its pre and post hooks.

for a middle

Explain the difference in reach: allowScripts filters dependencies' install scripts package by package, while ignore-scripts also silences the root project's own lifecycle and every hook.

for a senior

Trace the silent failures a shared ignore-scripts setting causes in CI, recover single packages with npm rebuild, and know the precedence of ignore-scripts, the bypass flag and the policy.

for a principal

Plan the move from a blanket ignore-scripts setting to an allowScripts policy, using the pending list to size the review work and keeping the root project's lifecycle intact.

## Two switches with different reach npm 12 gave projects a second way to control scripts, and it is easy to treat it as a synonym for the old one. It is not. - **`allowScripts`** is a field in the root `package.json`. Since npm 12.0.0, a **dependency's** install-time scripts — `preinstall`, `install`, `postinstall`, and `prepare` for non-registry sources — run only if this policy approves that package. Unapproved ones are skipped with a warning. The root project's own lifecycle scripts are outside the policy and still run. - **`--ignore-scripts`** (or `ignore-scripts=true` in an `.npmrc`) is a config switch that has existed for many releases. Its definition is blunt: npm does not run scripts specified in `package.json` files. That covers dependencies **and** the root project. | Setting | Dependencies' install scripts | Root lifecycle scripts | Script named in `npm run` / `npm test` | Its `pre` / `post` hooks | |---|---|---|---|---| | npm 12 default, empty policy | skipped, listed in a warning | run | runs | run | | `allowScripts` approves a package | that package's run | run | runs | run | | `strict-allow-scripts=true` | an unreviewed package fails the install | run | runs | run | | `--dangerously-allow-all-scripts` | all run, even denied ones | run | runs | run | | `--ignore-scripts` | none | none | runs | skipped | ## What still runs with `--ignore-scripts` Commands whose whole purpose is to run one named script keep doing that: `npm run <name>`, `npm test`, `npm start`, `npm stop` and `npm restart` still run the script you asked for. What they drop is the surrounding chain — `pretest`, `posttest`, `prebuild`, `postbuild` and the like do **not** run. ## What quietly stops working A CI pipeline that installs with `--ignore-scripts` usually "passes" the install step and then fails somewhere unrelated: - the root `prepare` script never runs, so Git hooks are not installed and any build placed in `prepare` is missing; - the root `postinstall` — patching, code generation, copying assets — is skipped; - native addons approved in `allowScripts` are not compiled, because the approval is overridden; - a later `npm run build` runs `build` but not `prebuild`, so generated files a build expects are absent. None of these produce an install error, which is why the setting is easy to forget once it lands in a shared `.npmrc`. ## Which setting wins The order is fixed by the config definitions: 1. `--ignore-scripts` overrides everything below it. 2. `--dangerously-allow-all-scripts` bypasses the `allowScripts` policy and runs every dependency install script, approved or denied — a migration escape hatch whose use npm strongly discourages. 3. `allowScripts` (with `strict-allow-scripts` deciding whether an unreviewed package is skipped or fails the install) is the normal policy. ## Finding out which switch is in force Both settings can come from several places, so check before you debug a missing build: - `npm config get ignore-scripts` prints the effective value, whichever `.npmrc` level or environment variable set it; - a CI job can set it without any file, because npm reads every `npm_config_*` environment variable as configuration — `npm_config_ignore_scripts=true` is enough; - the `allowScripts` field sits in the root `package.json`, and an `allow-scripts` line in an `.npmrc` is used only when that field is absent. ## Getting scripts back selectively `npm rebuild` exists for this case. After an install with `--ignore-scripts`, run `npm rebuild <pkg>` without the setting to execute that package's `preinstall`, `install` and `postinstall`; under npm 12 it runs the scripts the policy allows. `npm install-scripts ls` still lists unreviewed packages while `ignore-scripts` is set, so you can see what an allow-list would need to cover before you drop the blanket switch. ## Contrast with npm 11 and earlier Before npm 12 there was no default-deny policy, so `--ignore-scripts` was the only built-in way to stop dependency install scripts, and it was the switch people reached for. In npm 12 that job belongs to `allowScripts`, and `--ignore-scripts` remains what it always was: a switch that also turns off your own project's lifecycle. Moving from one to the other means listing what the blanket switch was hiding, approving what the build needs, and then deleting the switch. Whether either is an adequate security control is a separate question; here the point is what each one actually runs.

  • Did npm 12 change when the root project's own `preinstall` runs?
    Yes. Since npm 12.0.0 the root `preinstall` runs before dependencies are installed — under `npm ci`, after the lockfile is validated and `node_modules` is wiped — so it cannot `require()` a dependency on a fresh checkout. Setup that needs installed packages belongs in `install` or `postinstall`.
  • After `npm ci --ignore-scripts`, how do you build just the one native addon you need?
    Run `npm rebuild <pkg>` without `--ignore-scripts`. It runs that package's install lifecycle scripts and links its bins, and on npm 12 it runs them only if `allowScripts` approves the package, so approve it first if it is not already listed.

  • ignore-scripts only affects dependencies; the root project's prepare still runs
  • With ignore-scripts set, npm test refuses to run the test script
  • allowScripts also controls the root project's own lifecycle scripts
  • dangerously-allow-all-scripts overrides ignore-scripts when both are set
  • npm run build under ignore-scripts still runs prebuild first

Assumes npm 12 (anchor 12.2.0): the allowScripts default-deny policy, strict-allow-scripts and --dangerously-allow-all-scripts arrived in 12.0.0; --ignore-scripts behaves as in earlier releases but now also overrides that policy.