feat: add preunpack lifecycle script that runs before installation#8972
Open
DukeDeSouth wants to merge 1 commit intonpm:latestfrom
Open
feat: add preunpack lifecycle script that runs before installation#8972DukeDeSouth wants to merge 1 commit intonpm:latestfrom
preunpack lifecycle script that runs before installation#8972DukeDeSouth wants to merge 1 commit intonpm:latestfrom
Conversation
Adds a new `preunpack` lifecycle script that runs BEFORE dependencies are installed. This provides a hook for setup tasks that must happen before any packages are fetched, such as authenticating to private registries or validating environment prerequisites. Since npm v7, the `preinstall` script runs AFTER dependencies are installed (alongside `install` and `postinstall`), eliminating the ability to run scripts before the installation begins. This has been a pain point for 5 years (142 reactions, 72 comments on npm#2660). The `preunpack` script: - Runs on local `npm install` (without arguments) and `npm ci` - Does NOT run for specific package installs, global installs, or when --ignore-scripts is set - Aborts installation if the script exits with a non-zero code - Is listed as a known lifecycle script in `npm run` output Implements: npm/rfcs#403 Fixes: npm#2660 Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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