Yarn でパッケージをインストールする際に info There appears to be trouble with your network connection. Retrying… が出て失敗してしまう

Yarn でパッケージをインストールする際に

info There appears to be trouble with your network connection. Retrying…

が出て失敗してしまう現象に遭遇したので対処法をメモ。

現象

package.json が存在するプロジェクトで普通に yarn したら失敗しました。

> yarn

## 略

[2/4] Fetching packages...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
error An unexpected error occurred: "https://registry.yarnpkg.com/@material-ui/icons/-/icons-4.11.2.tgz: ESOCKETTIMEDOUT".
info If you think this is a bug, please open a bug report with the information provided in "PATH/TO/PROJECT/DIRECTORY/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

Webサイトは閲覧できるので接続自体は正常の模様。他の原因としては、「該当パッケージが重過ぎてダウンロード中にタイムアウトしてしまう」というケース。今回の場合は @material-ui/icons ですね。

対処

対処として、プロジェクト直下に .yarnrc というファイルを作成し、以下のように記述しました。

network-timeout 600000

単位はミリ秒なので、上記の場合は600秒、つまり10分となります。

今回はこれで上手く行きました。

その他の対策

ファイルを作成せずにコマンドの引数として渡すこともできるようです(未実施)。

> yarn config set network-timeout 600000

または

> yarn install --network-timeout 600000

参考

.yarnrc 設置

コマンドの引数

この記事を書いた人

アルム=バンド

フロントエンド・バックエンド・サーバエンジニア。LAMPやNodeからWP、Gulpを使ってejs,Scss,JSのコーディングまで一通り。たまにRasPiで遊んだり、趣味で開発したり。