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

Đang mở
#10,376 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@compiler-segfault đang làm issue này rồi.

Từ ngày 28/11/2026.

  • #10377 của @compiler-segfault — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, nodejs
Lĩnh vực
cli, developer-experience

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
JavaScript
Star
10.3k
Fork
5k
Merge trung bình
4 ngày 11 giờ
Pull request đã merge (30 ngày)
26

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của npm/cli

Tất cả issue của npm/cli

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.