LoginSignup
1

posted at

yarn でワークスペースにパッケージを追加できなくなった

表題の解決法メモ

$ yarn workspace hoge add fuga
error An unexpected error occurred: "expected workspace package to exist for \"@babel/helpers\"".

@babel/helpers の部分は人によって違うと思うが、ワークスペースにパッケージを追加しようとすると expected workspace package to exist for ... のエラーが出て追加できなくなってしまった
issueとしても挙げられている

環境

  • yarn 1.22.18
  • node v16.14.0
  • M1 MacBook Pro
  • macOS Monterey 12.4

原因

https://github.com/yarnpkg/yarn/issues/7807#issuecomment-623223536
複数のパッケージが依存しているパッケージのバージョンが異なっている

解決策

いくつか解決策がある

その1 yarn-deduplicate を実行する

https://github.com/yarnpkg/yarn/issues/7807#issuecomment-1097198463
これが一番正攻法な気がする

$ npx yarn-deduplicate && yarn install

yarn dedupe では解決できない
error The dedupe command isn't necessary. 'yarn install' will already dedupe. と出てしまう

その2 一旦ロックファイルを削除する

最も簡単だが yarn-deduplicate よりも差分が大きい

その3 yarn のバージョンを下げる

https://zenn.dev/kyo9bo/articles/063e60c19449fb
yarn のバージョン1.19あたりから発生しているエラーらしく1.18.0にすることでも回避できる

$ yarn --version
1.22.18
$ yarn policies set-version 1.18.0
$ yarn --version
1.18.0

この場合 .yarnrc.yarn/releases/yarn-1.18.0.cjs の2ファイルが作成される

その4 resolutions を使う

https://github.com/yarnpkg/yarn/issues/7807#issuecomment-643642616

package.json
{
  "resolutions": {
    "@babel/helpers": "7.17.9"
  }
}

エラーの原因となっているパッケージ全て記述する必要があり面倒

その5 package.json に手動で書き足して yarn install

根本的な解決になっていないのでおすすめはしない

おわりに

以上、メモ書きでした

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
1