[RRFC] Reintroduce global-only bin command

オープン
#657 コメント 12 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
javascript
領域
cli

調査の方向性

まず削除された npm bin -g の挙動と、参照されている npm/cli の PR 5459 を確認し、次に提案されている npm prefix -g と npm path のエントリーポイントを比較します。グローバル実行可能ファイルの場所を出力し、それが PATH に存在するかどうかを報告する実装について合意できれば完了です。Windows の場合も含みます。

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

説明

discuss semver:minor

Motivation ("The Why")

With npm v9, the npm bin command was removed, which also removed the npm bin -g command. It displayed the path for globally-installed packages, and also checked to make sure that that path was included in the $PATH environment variable. For unix/mac, this is generally not an issue as global packages are installed to the same location as the node/npm binaries.

However, with Windows it's different as the global packages are installed in a completely separate directory from the node installation. Being able to run node does not necessarily mean that the user is also able to run the globally installed packages. Taken from the Windows installer, updating the $PATH variable is split into two different "features".

* Add to PATH
  * Node.js and npm - Add Node.js and npm (if installed) to the PATH environment variable
  * npm modules - Add modules that are installed globally by npm to the PATH environment variable. This option works for the current user only.

With a clean install of node on Windows with these two features selected, it will add two separate locations to the $PATH:

C:\Program Files\nodejs
C:\Users\<username>\AppData\Roaming\npm

This is because of the value of prefix decided by the builtin config, which is set to %APPDATA%\npm by default:

$ npm config ls
; "builtin" config from C:\Program Files\nodejs\node_modules\npm\npmrc

prefix = "C:\\Users\\<username>\\AppData\\Roaming\\npm"

If I then install a package (ex: typescript) with npm i -g typescript, this installs tsc to C:\Users\<username>\AppData\Roaming\npm\tsc which I'm only able to invoke successfully because of the second new entry in my PATH variable. This logic is entirely custom to npm, as it's derived from npm's prefix config value.

This all works seamlessly in the ideal case, but speaking as someone that handles troubleshooting for a large population of developers that are not always primarily doing Node/npm/JS development, incomplete or inaccurate $PATH variables have been an uncommon yet consistent issue. It was useful for us to have npm bin -g as that could prompt the user to add the global package location to their $PATH if it wasn't already set (as it would warn with (not in PATH env variable)).

Given the nature of Windows' two-location installation, this does seem like a Windows-only problem. As such, I'm not sure what the cleanest fix would be. For this use case we do not need any of the "non-global" functionality of npm bin without -g that allowed for the aforementioned bad behavior, so I'm not suggesting that this command be added back as it was. If the functionality to check the global install location (and also if that was in the $PATH variable) could be added to an existing command or even a new command, that would work well for us. I'd be happy to contribute such a feature (whatever it may look like) if that sounds like a good idea to all involved.

Example

This RFC would be for a new command, subcommand, or flag that reproduces the behavior of npm bin -g. Specifically, printing the location of globally installed executable packages and checking whether that location is available in the $PATH environment variable.

Potential suggestions:

  • Add this as a subcommand/flag to npm prefix -g as this path is derived from the prefix config value.
  • Make a new command that is responsible for $PATH-related logic
  • Something else entirely?
How
Current Behaviour

No platform-independent way to calculate location of globally installed packages.

Desired Behaviour
$ npm prefix bin -g
$ npm path
$ npm path bin
C:\Users\<username>\AppData\Roaming\npm
(not in PATH env variable)
References
主要言語
JavaScript
スター
780
フォーク
266
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

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

npm/rfcs のほかの issue

npm/rfcs の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

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

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