💻

環境構築|corepack enable yarnしてもCLIに怒られる(This project's package.json defines "packageManager": "yarn@*.*.*". However the current global version of Yarn is *.**.**.)

    環境構築

corepack enable yarnをしてからyarnのコマンドを使おうとすると下記のエラーとメッセージが出ます。

error This project's package.json defines "packageManager": "yarn@4.2.2". However the current global version of Yarn is 1.22.22.

Presence of the "packageManager" field indicates that the project is meant to be used with Corepack, a tool included by default with all official Node.js distributions starting from 16.9 and 14.19.
Corepack must currently be enabled by running corepack enable in your terminal. For more information, check out https://yarnpkg.com/corepack.

エラーの意味

package.json"packageManager": "yarn@4.2.2"が定義されているのに、現在のYarnのバージョンが1.22.22なのでエラーを出したよ。という意味。

一応補足ですが、corepackとはパッケージマネージャーを管理するツールで、指定したパッケージマネージャを自動で使わせたり、指定外のパッケージマネージャを使えなくしたりできるもので、エラー下のメッセージの通り、Node.jsの16.914.19以降ではデフォルトで搭載されています。(corepack -vを叩けば返答をくれるはず)詳細は下記の記事が参考になります。

corepack is 何?|Zenn

それで、そのパッケージマネージャーの制約の通りバージョン不一致のエラーを出してくれていた訳です。

ちなみに、corepack use yarn@4.2.2をすると、yarn@4.2.2を使ってpackage.json記載のパッケージがインストールされ、node_modulesに入れてくれます。

現状と前提

現状として、corepackのGitHubに記載の通り下記の手順は試しています。

npm uninstall -g yarn pnpm
npm install -g corepack

それから、package.jsonは下記の通り一番下にpackageManagerプロパティを設定済み。外部パートナーが組んでくれたようで、キャッシュは付いていませんが。

{
  ...
  "packageManager": "yarn@4.2.2"
}

それで、corepack enable yarnを叩くと、下記の通り、特に何も返答がありません。(成功したのか?ん?という感じ)

user % corepack enable yarn
# 特に何の返答もなし...
user %

yarn -vすると、1.22.22のまま。

エラー(結局エラーじゃない)の原因

原因はシンプルにパスの問題でした。下記それぞれの実行結果を見てください。

$ which yarn
# /opt/homebrew/bin/yarn
$ which corepack
# /user/.nodenv/shims/corepack
$ which node
# /user/.nodenv/shims/node

nodeやcorepackは.nodenv/shimsにパスが向いているのに、yarnだけhomebrewに向いています。ということは、corepack enable yarnコマンドの状況は下記だったということ。

  • corepackとしてはcorepack enable yarnは成功している
  • なのでcorepack use yarn@4.2.2も当然成功する
  • なのにCLIが使うyarnのバージョンは1.22.22のままだからcorepackがエラーを出してくれた

corepack enable yarnをしたあと特に何も反応がなかったのでちょっと混乱しちゃいましたが、yarnもnodenv経由で使ってやれば解決しました。

ちなみにこの点については、ちゃんとGitHubで忠告してくれていました。

First uninstall your global Yarn and pnpm binaries (just leave npm). In general, you'd do this by running the following command:

npm uninstall -g yarn pnpm

# That should be enough, but if you installed Yarn without going through npm it might
# be more tedious - for example, you might need to run `brew uninstall yarn` as well.

corepack|GitHub

訳すると、「まずはグローバルなYarnやpnpmを消しなよ。このコマンドで十分なはずだが、npmを介さずにYarnをインストールした場合もっと面倒かも。例えば、brew uninstall yarnも必要かも。」

corepackを使うということは、 yarn や pnpm の管理をcorepackに委ねるということなので、グローバルでインストールしたyarnやpnpmはお掃除しておかないといけません。

Yarnをグローバルでインストールしない方が良い理由は、Yarnのcorepackページでも解説がされています。

You may notice by reading our installation guide that we don't tell you to run npm install -g yarn to install Yarn - we even recommend against it. The reason is simple: just like your project dependencies must be locked, so should be the package manager itself.
corepack|Yarn

解決方法

そもそも、nodenvやnodebrewでnodeのバージョン管理をしているなら、それら経由でYarnをインストールしてパスを向けておいた方が安牌です。

  • brew uninstall yarnでhomebrewのYarnをアンインストール
  • 念の為brew cleanupでお掃除
  • yarn -vcommand not found: yarnを確認

それからnodenv installで自動でYarnもインストールしてくれるプラグインをもう一度整えます。(Macの構築当初やっていたはずが、なぜか使えない状況になっていた)

nodenv-yarn-install|GitHub

$ mkdir -p "$(nodenv root)/plugins"
$ git clone https://github.com/pine/nodenv-yarn-install.git "$(nodenv root)/plugins/nodenv-yarn-install"

nodenvのルートパス直下のpluginsディレクトリの中でクローンしてやるだけでOK!クローンできたら、nodenvで参照していたnodeバージョンを一度uninstallして、もう一度nodenv install [version]すれば...

...
Installing Yarn...
Installed Yarn 1.22.22

ときちんとyarnもインストールされていて、which yarnすれば/user/.nodenv/shims/nodeが返ってきたので一件落着!