[BUG] `--no-global` on the command line still installs globally when the environment or an npmrc sets `location=global`

オープン
#10,376 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@compiler-segfault がすでに取り組んでいます。

2026年11月28日 から。

  • #10377 @compiler-segfault による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, nodejs

調査の方向性

Start with the global getter in lib/npm.js (around lines 445-447), which reads both global and location, then the flat location option in workspaces/config/lib/definitions/definitions.js and the global handling in lib/commands/install.js. Check how config sources are ordered in docs/lib/content/using-npm/config.md. Run the linked repro.mjs against a local checkout; done means all eleven cases report the highest-priority setting, with the global-versus-location precedence resolved consistently.

索引モデルが issue の本文から書いたものです。

説明

Bug Needs Triage
Is there an existing issue for this?
  • I have searched the existing issues
This issue exists in the latest npm version
  • I am using the latest npm
This is not just a request to bump a dependency for a CVE
  • This is not solely a request to bump a dependency for a CVE
Current Behavior

npm turns global mode on when global is true or location is global (npm.global, which npm install and npm prefix read, and location's flat option), and each of the two is read from the highest source that sets it. When they come from different sources, the lower one wins whenever it says global:

  • npm install --no-global, or --global=false, installs into the global prefix when the environment has npm_config_location=global or an npmrc has location=global.
  • npm install --location=project installs into the global prefix when the environment has npm_config_global=true.
  • npm install installs into the global prefix when an npmrc has global=true and the environment npm_config_location=project, or an npmrc has location=global and the environment npm_config_global=false.

npm prefix prints the global prefix in each of them. The config docs sort npm's sources by priority: command-line flags, then environment variables, then npmrc files.

Expected Behavior

Global mode follows whichever of global and location comes from the higher-priority source, so each command above installs into the project, and npm prefix prints the project. Where only one of the two is set, nothing changes: --no-global still wins over npm_config_global=true, and --location=project over npm_config_location=global. Where the higher-priority one says global, npm still installs globally, as --global does over npm_config_location=project.

Steps To Reproduce
  1. Fetch repro.mjs at the revision I ran (curl.exe in Windows PowerShell, where curl is Invoke-WebRequest):

    curl -LO https://gist.githubusercontent.com/YevheniiKotyrlo/945de540b5818e71bbe71b0a8277ed6e/raw/06ec11a803c54459afa383139a9f3c9952015b92/repro.mjs
    
  2. Run it with a Node that npm 12 runs on:

    node repro.mjs
    

    With no argument it installs the latest npm below its temporary folder, with the npm that comes with Node; node repro.mjs <version> installs that version, and node repro.mjs <path to bin/npm-cli.js> runs a checkout.

It runs eleven cases, each in a folder of its own holding a project, a folder dependency beside it, a global prefix and a user npmrc. Each case puts global and location on the command line, in the environment or in that npmrc, and runs npm prefix and npm install ../dependency in the project, offline, with an empty global config file, a new cache and none of the caller's npm_config_* variables. It then reports where npm prefix pointed and where the dependency went. It exits 1 when, in a case where a lower-priority source says global against a higher one, npm goes where the lower one says, 0 when every case goes where its highest setting says, and 2 when it cannot tell: a case goes anywhere else, or a step fails.

On Windows 11 with npm 12.2.3:

node v26.12.1 on win32 x64

# with the npm that comes with node, in <tmp>\npm-install
$ npm install npm@latest
installed: <tmp>\npm-install\node_modules\npm\bin\npm-cli.js

$ npm --version
12.2.3

[…]

# case 6: npm_config_location=global, --no-global; expected: the project
# in <tmp>\case-6: project/, dependency/, global-prefix/, user.npmrc (empty)
$ npm_config_location=global npm prefix --no-global
<tmp>\case-6\global-prefix
$ npm_config_location=global npm install ../dependency --no-global
added 1 package in 1s
# npm prefix: the global prefix; the dependency: in the global prefix

[…]

# where each case went (npm prefix; npm install ../dependency)
nothing set: project; project: as expected
npm_config_location=global: global prefix; global prefix: as expected
npm_config_global=true, --no-global: project; project: as expected
npm_config_location=global, --location=project: project; project: as expected
npm_config_location=project, --global: global prefix; global prefix: as expected
npm_config_location=global, --no-global: global prefix; global prefix: NOT as expected: followed the lower setting (expected: project)
npm_config_location=global, --global=false: global prefix; global prefix: NOT as expected: followed the lower setting (expected: project)
location=global in the user npmrc, --no-global: global prefix; global prefix: NOT as expected: followed the lower setting (expected: project)
npm_config_global=true, --location=project: global prefix; global prefix: NOT as expected: followed the lower setting (expected: project)
global=true in the user npmrc, npm_config_location=project: global prefix; global prefix: NOT as expected: followed the lower setting (expected: project)
location=global in the user npmrc, npm_config_global=false: global prefix; global prefix: NOT as expected: followed the lower setting (expected: project)

RESULT (exit 1): defect shows (npm 12.2.3): in 6 of the 6 cases where a lower-priority source says global against a higher one, global mode followed the lower one

The latest branch at 787f1520, run from a clone, ends the same way, and so do npm 11.20.0 and 10.2.4, 12.2.3 and that branch on Linux, in Docker's node:26 image, and npm 12.2.1 on macOS.

Environment
  • npm: 12.2.3 on Windows and Linux, 12.2.1 on macOS
  • Node.js: 26.12.1 on Windows and macOS, 26.13.1 on Linux
  • OS Name: Windows 11 Pro 10.0.26300; Debian, in Docker's node:26 image; macOS 27.2.1
  • System Model Name: a desktop PC (Windows and Linux); a MacBook Air (macOS)
  • npm config:
; the reproducer runs npm with an empty global config file, a user config file of each case's own, these through the environment, and each case's own global and location
audit = false
cache = "<tmp>\npm-cache"
fund = false
offline = true
prefix = "<tmp>\case-<n>\global-prefix"
update-notifier = false
主要言語
JavaScript
スター
10.3k
フォーク
5k
平均マージ
4日 11時間
マージ済み PR(30日)
26

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

npm/cli のほかの issue

npm/cli の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。