Skip to content

fix(install): repeat specs in global allow-scripts suggestion - #9840

Open
Ashish-CodeJourney wants to merge 1 commit into
npm:latestfrom
Ashish-CodeJourney:fix/9835-global-allow-scripts-suggestion
Open

fix(install): repeat specs in global allow-scripts suggestion#9840
Ashish-CodeJourney wants to merge 1 commit into
npm:latestfrom
Ashish-CodeJourney:fix/9835-global-allow-scripts-suggestion

Conversation

@Ashish-CodeJourney

Copy link
Copy Markdown

The blocked-install-scripts warning suggested npm install -g --allow-scripts=<pkg>, which has no install targets, so the command falls back to installing the current directory and fails with ENOENT reading package.json for anyone not sitting in a project.

Build the suggestion from the command that was actually run and its positional specs, so npm install -g esbuild now suggests npm install -g esbuild --allow-scripts=esbuild. Commands invoked without specs (npm update -g) keep the bare form, which works.

Fixes: #9835

What / Why

Global installs have no project package.json, so npm install-scripts approve cannot be used. The warning instead points at --allow-scripts, but the command it prints is not runnable:

$ npm install -g esbuild

added 2 packages in 3s
npm warn install-scripts 1 package had install scripts blocked because they are not covered by allowScripts:
npm warn install-scripts   esbuild@0.28.1 (postinstall: node install.js)
npm warn install-scripts
npm warn install-scripts Run `npm install -g --allow-scripts=esbuild` to allow these scripts once, or `npm config set allow-scripts=esbuild --location=user` to allow them for all global installs.

$ npm install -g --allow-scripts=esbuild
npm error code ENOENT
npm error syscall open
npm error path /tmp/tmp.j1pwe1SPOy/package.json
npm error errno -2
npm error enoent Could not read package.json: Error: ENOENT: no such file or directory, open '/tmp/tmp.j1pwe1SPOy/package.json'

npm install -g with no positional specs installs the current directory, which a global installer is generally not sitting in, so the suggested remediation dead-ends on ENOENT.

How

remediationLines() in lib/utils/reify-output.js no longer hardcodes npm install -g. It now calls a new globalAllowScripts() helper, added next to the existing configSetAllowScripts() in lib/utils/allow-scripts-remediation.js, which rebuilds the command from npm.command and the positional specs in npm.argv:

$ npm install -g esbuild
npm warn install-scripts Run `npm install -g esbuild --allow-scripts=esbuild` to allow these scripts once, or `npm config set allow-scripts=esbuild --location=user` to allow them for all global installs.

$ npm install -g esbuild --allow-scripts=esbuild

changed 2 packages in 398ms

Commands invoked without positional specs (e.g. npm update -g) keep the bare npm update -g --allow-scripts=... form, which needs no package.json and already works.

The same non-working example was documented in npm install-scripts and npm approve-scripts; both are corrected.

Tests

Two tests added to test/lib/utils/reify-output.js, written failing first:

  • global install repeats the requested specs in the suggestion
  • global command without specs suggests that command

lib/utils/reify-output.js and lib/utils/allow-scripts-remediation.js remain at 100% coverage. test/lib/utils/*, test/lib/commands/install*, update, ci and docs all pass.

References

Fixes #9835

The blocked-install-scripts warning suggested `npm install -g
--allow-scripts=<pkg>`, which has no install targets, so the command
falls back to installing the current directory and fails with ENOENT
reading package.json for anyone not sitting in a project.

Build the suggestion from the command that was actually run and its
positional specs, so `npm install -g esbuild` now suggests `npm install
-g esbuild --allow-scripts=esbuild`. Commands invoked without specs
(`npm update -g`) keep the bare form, which works.

Fixes: npm#9835
@Ashish-CodeJourney
Ashish-CodeJourney requested review from a team as code owners August 4, 2026 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Incomplete remediate prompt npm install -g --allow-scripts=<package>

1 participant