feat: add preunpack lifecycle script that runs before installation
#8972
+239
−2
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.
Summary
Since npm v7, the
preinstallscript runs after dependencies are installed — making it functionally identical topostinstalland eliminating the ability to run scripts before installation begins. This has been an open bug for 5 years (#2660, 142 reactions, 72 comments,Priority 1label).This PR implements RFC #403 by adding a new
preunpacklifecycle script that runs BEFOREarb.reify()— i.e., before any dependencies are fetched or installed.Problem
Users cannot:
Solution
Behavior
preunpackruns?npm install(no args, local)npm cinpm install <pkg>npm install -g--ignore-scriptspreunpackin package.jsonExample usage
{ "scripts": { "preunpack": "aws codeartifact login --tool npm --repository my-repo --domain my-domain" } }Changes
lib/commands/install.jspreunpackexecution beforeArboristrequirelib/commands/ci.jspreunpackexecution beforearb.reify()lib/commands/run.jspreunpackto known lifecycle scripts listdocs/.../scripts.mdpreunpackevent and update operation ordertest/.../install.jstest/.../ci.js+239 lines, -2 lines. Non-breaking change.
Prior Art
preunpack(discussed in npm meeting 2021-07-21)preinstallbefore reify (closed: breaking change)prefetchscript (closed: no response)preinstallOnly(closed: abandoned)owlstronaut/feat-preunpack(2025-06-19) — Started implementation but never mergedTest Plan
npm install— preunpack runs first in lifecycle order (9/9 tests pass)npm ci— preunpack runs first in lifecycle order (12/12 tests pass)npm install— preunpack runs beforearb.reify()npm ci— preunpack runs beforearb.reify()npm install <pkg>— preunpack does NOT run--ignore-scripts— preunpack does NOT runFixes #2660
Implements npm/rfcs#403
Made with Cursor