GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix!: run root preinstall before reify by owlstronaut · Pull Request #9267 · npm/cli

Conversation

@owlstronaut

@owlstronaut owlstronaut commented Apr 21, 2026

Copy link
Copy Markdown

BREAKING CHANGE: root `preinstall` now runs before dependencies are installed.

Summary

Moves the root preinstall script back to its documented position: before dependencies are installed. Restores behavior that shipped through npm 6 and was unintentionally broken in npm 7 when Arborist took over reify.

Fixes #2660.

The bug

The scripts docs have always said preinstall runs "before the package is installed." Since npm 7, the root package.json's preinstall has actually run after arb.reify() i.e. after every dependency has been resolved, fetched, and unpacked. Arborist explicitly excludes the project root from its rebuild queue (reify.js:1234, !diff.ideal.isProjectRoot), and lib/commands/install.js appended preinstall to the post-reify lifecycle loop along with install, postinstall, prepublish, preprepare, prepare, postprepare. npm ci has the same shape.

Net effect: there is currently no way to run a script at the root before dependencies hit disk. Projects that want to bootstrap auth, generate files consumed during resolution, or gate installs behind a precondition have had no supported hook for five years.

The fix

Split the root lifecycle loop in two:

  1. preinstall runs via a small helper before arb.reify().
  2. install, postinstall, prepublish, preprepare, prepare, postprepare run after reify, as they did before.

Same split applied to npm ci.

Why now, and why not wait for a lifecycle redesign

This has history:

  • Aug 2020 - [BUG] Preinstall script runs after installing dependencies #2660 filed by @ljharb.
  • Feb 2021 - fix(preinstall): run preinstall before reify #2713 opened by @wraithgar with essentially this same code change, then self-closed as "too breaking without broader design work."
  • Jul 2021 - cli team meeting decided to revert preinstall ordering in the next major.
  • 2021 - RFC #403 (preunpack) and RFC #437 (preinstall revert) opened. Neither progressed. RFC 403's preunpack hook was rejected in triage as not solving the actual bug.
  • 2021–2025 - Tracker went cold.
  • Feb 2026 - feat: add preunpack lifecycle script that runs before installation #8972 opened implementing the rejected RFC 403 preunpack approach. Close but not the right fix.

A comprehensive lifecycle redesign is still the right long-term project and has clearly not happened in five years. Gating the revert on it was reasonable in 2021; but has proven that continuing to gate on it in 2026 just means users keep hitting a bug the docs promise doesn't exist. npm 12 is the right window to ship the scoped revert and decouple it from a future lifecycle rewrite.

The code change here is functionally the same as #2713 The differences are: it also patches npm ci, updates the docs to match reality, adds regression tests, and ships in a major (npm 12) rather than being blocked on a design gate that no longer has owners.

Closes / supersedes

When this lands:

  • Closes [BUG] Preinstall script runs after installing dependencies #2660
  • Closes feat: add preunpack lifecycle script that runs before installation #8972 (supersedes the preunpack approach)
  • Closes RFC: Add preunpack life cycle script rfcs#403 (supersedes — preunpack not needed)
  • Resolves RFC: Robust Lifecycle Scripts rfcs#437 (the scoped revert it proposed)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

firefoxic, dossy, tomer953, and ahrjarrett reacted with thumbs up emoji dossy reacted with hooray emoji dossy reacted with heart emoji dossy reacted with rocket emoji
All reactions
  • 👍 4 reactions
  • 🎉 1 reaction
  • ❤️ 1 reaction
  • 🚀 1 reaction
@owlstronaut
owlstronaut requested a review from a team as a code owner April 21, 2026 21:43
@owlstronaut owlstronaut mentioned this pull request Apr 21, 2026
npm 12 npm/statusboard#1068
Closed
2 tasks
nishantms
nishantms reviewed May 18, 2026
View reviewed changes
Comment thread lib/commands/ci.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread test/lib/commands/ci.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread lib/commands/install.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@owlstronaut
owlstronaut force-pushed the fix/preinstall-ordering-2660 branch from 4ce2da1 to 586dd87 Compare May 20, 2026 16:44
@owlstronaut
owlstronaut requested a review from a team as a code owner May 20, 2026 16:44
@owlstronaut
owlstronaut requested a review from nishantms May 20, 2026 16:47
wraithgar
wraithgar reviewed May 20, 2026
View reviewed changes
Comment thread docs/lib/content/using-npm/scripts.md
* `postprepare`

`preinstall` runs before any dependencies are fetched or unpacked into `node_modules`, so scripts can prepare the environment (for example, setting up authentication for a private registry) before resolution begins. The remaining scripts run after installation has completed.

@wraithgar wraithgar May 20, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this means any scripts that require() a dependency will probably fail

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@wraithgar wraithgar May 20, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the biggest reason for the change in the first place. This tripped up more folks than anticipated. It seems obvious in hindsight but it isn't.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

owlstronaut reacted with heart emoji
All reactions
  • ❤️ 1 reaction

@nishantms nishantms May 21, 2026

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 — worth calling out explicitly in the doc since this is the historical footgun. Maybe a sentence at the end of the new paragraph, e.g.:

Because dependencies are not yet on disk when preinstall runs, scripts cannot require() any package from node_modules at this point. Use install / postinstall for setup that depends on installed packages.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gorkemmulayim

This comment was marked as off-topic.

Sign in to view
nishantms
nishantms reviewed May 21, 2026
View reviewed changes
Comment thread test/lib/commands/install.js
t.ok(post, 'postinstall ran')
t.equal(pre.depInstalled, false, 'preinstall runs before dependencies are installed')
t.equal(post.depInstalled, true, 'postinstall runs after dependencies are installed')
})

@nishantms nishantms May 21, 2026

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit — the new test covers the success path; should we also lock in the failure contract? Something like:

await t.test('a failing preinstall prevents reify', async t => {
  // mock '@npmcli/run-script' to throw on preinstall
  // assert npm.exec('install') rejects AND node_modules/abbrev does not exist
})

The "no dep on disk before preinstall" guarantee is the real reason for the change — worth a regression test so a future refactor cannot quietly swallow a rejection and still reify.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
nishantms
nishantms reviewed May 21, 2026
View reviewed changes
Comment thread test/lib/commands/ci.js Outdated
'package-lock.json': JSON.stringify(packageLock),
},
mocks: {
'@npmcli/run-script': (opts) => {

@nishantms nishantms May 21, 2026

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit — the install.js equivalent mocks this as async (opts) => {...}; here it is sync. Both work, but matching the shape would make it obvious the helper returns a promise:

'@npmcli/run-script': async (opts) => { ... },

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
nishantms
nishantms previously approved these changes May 21, 2026
View reviewed changes

@nishantms nishantms left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, no major blocker, few nits

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Michael Smith
fix!: run root preinstall before reify
9ac918f
BREAKING CHANGE: root \`preinstall\` now runs before dependencies are installed.
@owlstronaut
owlstronaut dismissed nishantms’s stale review via 9ac918f May 21, 2026 18:29
@owlstronaut
owlstronaut force-pushed the fix/preinstall-ordering-2660 branch from 586dd87 to 9ac918f Compare May 21, 2026 18:29
nishantms
nishantms approved these changes May 21, 2026
View reviewed changes
@owlstronaut
owlstronaut merged commit 2a03860 into latest May 21, 2026
25 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@owlstronaut
owlstronaut deleted the fix/preinstall-ordering-2660 branch May 21, 2026 18:37
@github-actions github-actions Bot mentioned this pull request May 21, 2026
chore: release 12.0.0-pre.1 #9387
Merged
@npm-cli-bot npm-cli-bot mentioned this pull request Jun 23, 2026
deps: upgrade npm to 12.0.0-pre.1 nodejs/node#64096
Closed
This was referenced Aug 9, 2026
manual song post creation jtzero/cle-gunners#435
Merged
Hh tinysecrets/realwah-lah.com#1
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@nishantms nishantms nishantms approved these changes
+1 more reviewer
@wraithgar wraithgar wraithgar left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[BUG] Preinstall script runs after installing dependencies

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@owlstronaut @gorkemmulayim @wraithgar @nishantms
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.