【Yarn】YN0002対処法

## はじめに

Yarn berryで`yarn install`などを実行すると`YN0002`のメッセージが表示されることがあります。  

```terminal
$ yarn install
➤ YN0000: ┌ Resolution step
➤ YN0002: │ @devtools-ds/themes@npm:1.2.0 [b63e6] doesn't provide react-dom (pha6g3), requested by @design-systems/utils
➤ YN0000: │ Some peer dependencies are incorrectly met; run yarn explain peer-requirements <hash> for details, where <hash> is the six-letter p-prefixed code
➤ YN0000: └ Completed in 0s 286ms
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed in 2s 412ms
➤ YN0000: ┌ Link step
➤ YN0000: │ ESM support for PnP uses the experimental loader API and is therefore experimental
➤ YN0000: └ Completed in 0s 412ms
➤ YN0000: Done with warnings in 3s 471ms
```

`YN0002`が出力されないようにする方法について記載します。  

`YN0002`の詳細は以下をご参照ください。

https://yarnpkg.com/advanced/error-codes#yn0002---missing_peer_dependency

## 対処法

`YN0002`の詳細を確認するために以下のコマンドを実行します。

```terminal
yarn explain peer-requirements <hash>
```

`<hash>`は`YN0002`のメッセージ中の`()`内に記載されている`p`から始まる文字列に置き換えます。  

```terminal
YN0002: │ @devtools-ds/themes@npm:1.2.0 [b63e6] doesn't provide react-dom (pha6g3), requested by @design-systems/utils
```

上記の場合には`pha6g3`に置き換えます。

```terminal
$ yarn explain peer-requirements pha6g3
➤ YN0000: @devtools-ds/themes@npm:1.2.0 [b63e6] doesn't provide react-dom, breaking the following requirements:

➤ YN0000: @design-systems/utils@npm:2.12.0 [d4d01] → >= 16.8.6 ✘
```

実行して得られた結果をもとに`yarnrc.yml`へ`packageExtensions`を追記します。  

https://yarnpkg.com/configuration/yarnrc#packageExtensions

```yarnrc.yml
packageExtensions:
  '@devtools-ds/themes@1.2.0':
    peerDependencies:
      react-dom: '>= 16.8.6'
```

再度`yarn install`を実行します。  

```terminal
$ yarn install
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed in 0s 293ms
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed in 1s 582ms
➤ YN0000: ┌ Link step
➤ YN0000: │ ESM support for PnP uses the experimental loader API and is therefore experimental
➤ YN0000: └ Completed in 0s 566ms
➤ YN0000: Done with warnings in 2s 806ms
```

`YN0002`は出力されなくなりました。  

また`yarn explain peer-requirements <hash>`を実行した際に以下のように出力される場合があります。  
  
```terminal
$ yarn install
...
➤ YN0002: │ add_storybook2@workspace:. doesn't provide require-from-string (p5256f), requested by @storybook/react
...

$ yarn explain peer-requirements p5256f
➤ YN0000: add_storybook2@workspace:. doesn't provide require-from-string, breaking the following requirements:

➤ YN0000: @storybook/react@npm:6.5.13 [5cbf9] → ^2.0.2 ✘
```

この場合には不足しているものをインストールすると解消されます。  

```terminal
$ yarn add -D require-from-string

$ yarn explain peer-requirements p5256f
➤ YN0000: add_storybook2@workspace:. provides require-from-string@npm:2.0.2 with version 2.0.2, which satisfies the following requirements:

➤ YN0000: @storybook/react@npm:6.5.13 [5cbf9] → ^2.0.2 ✓

$ yarn install
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed in 0s 288ms
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed in 1s 621ms
➤ YN0000: ┌ Link step
➤ YN0000: │ ESM support for PnP uses the experimental loader API and is therefore experimental
➤ YN0000: └ Completed in 0s 444ms
➤ YN0000: Done with warnings in 2s 737ms

```

## 警告を非表示にする

`yarnrc.yml`の`logFilters`で警告を非表示にすることもできます。  

https://yarnpkg.com/configuration/yarnrc#logFilters

```yarnrc.yml
logFilters:
  - code: 'YN0002'
    level: discard
```