できない.dev

npm「Fetching packages of type "git" have been disabled」の原因と直し方

npm の Fetching packages of type "git" have been disabled(EALLOWGIT)は、.npmrc に allow-git=root を書けば直る。
npm 12 で allow-git の既定が none になり、git の依存を入れなくなった。

公開:

要約

npm 12 で、github: や git+https: で書いた依存を含むプロジェクトに npm ci や npm install を打つと、終了コード 1 で止まる。

npm error code EALLOWGIT
npm error Fetching packages of type "git" have been disabled

Fetching packages of type "git" have been disabled は、git のリポジトリから取る依存を npm が拒んだという意味である。
npm 12 から、設定 allow-git の既定が none になった。
プロジェクトの .npmrc に allow-git=root を書けば、package.json に書いた git の依存は入る。

解決策

1. .npmrc に allow-git=root を書く

# .npmrc(package.json と同じ場所)
allow-git=root

.npmrc をコミットすれば、手元でも CI でも同じ設定で npm ci が通る。

2. コマンドに付けて 1 回だけ許す

npm install --allow-git=root "github:jonschlinkert/is-odd#3.0.1"
npm ci --allow-git=root

npm 12.2.2 では、どちらも終了コード 0 で通った。
この指定はそのコマンドにしか効かないので、次に何も付けずに npm ci を打つと、また EALLOWGIT で止まる。

3. 間接の git の依存は all にするか替える

allow-git に指定できる値は none・root・all の 3 つである。
依存の依存に git の依存があるなら、allow-git=all にするか、その依存をレジストリに公開された版に替える。
all はすべての git の依存を許すので、塞いだ経路も開くことになる。

ドキュメントによると、allow-git を変えても、入れ済みの依存は消えない。
URL で書いた依存は、同じ 12 で EALLOWREMOTE(Fetching packages of type "remote" have been disabled)になり、allow-remote で同じように許す。

よくある原因

  1. npm を 12 に上げた。
    11 は github:jonschlinkert/is-odd#3.0.1 のような依存を何も言わずに入れていたので、上げた直後にこのエラーで気づく。
    npm 12 の予告(新しいタブで開く)は、git の依存に含まれる .npmrc が git の実行ファイルを差し替えられる経路を塞ぐための変更だと説明している。
  2. 手元と CI で npm の版が違う。
    手元の npm 11 で作ったロックファイルを、npm 12 の入った CI で npm ci すると止まる。
    このとき拒まれる行は git+ssh:// で始まる解決済みの URL になる。
  3. git の依存が依存の依存に入っている。
    設定のドキュメント(新しいタブで開く)によると、root が許すのはプロジェクトの package.json に書いた git の依存だけである。
    このとき出る文言は Fetching non-root packages of type "git" have been disabled になる。