Next.jsのサイトをVercelでホスティングしているのですが、費用を削減するためにCloudflare pagesにデプロイしようとしたらエラーが出てしまったのでその解消方法についての記事です

エラー内容

リポジトリにCloudflare pagesを接続して、pushするたびにデプロイされるように設定したところエラーが発生しました

このエラーはlocal環境とCloudflareのyarnのバージョンが異なることで、再インストールしようとしたけど、編集が禁止されてるので継続できない、という内容です

12:32:26.692	Cloning repository...
12:32:28.022	From https://github.com/masahiro04/okbcorp
12:32:28.023	 * branch            a4aac053ac2322d7e02e56dfc237f162a957589a -> FETCH_HEAD
12:32:28.023	
12:32:28.069	HEAD is now at a4aac05 chore: buildを除外
12:32:28.069	
12:32:28.168	
12:32:28.168	Using v2 root directory strategy
12:32:28.193	Success: Finished cloning repository files
12:32:29.809	Checking for configuration in a wrangler.toml configuration file (BETA)
12:32:29.810	
12:32:29.810	Found wrangler.toml file. Reading build configuration...
12:32:29.814	pages_build_output_dir: build
12:32:29.814	Build environment variables: (none found)
12:32:29.911	Successfully read wrangler.toml file.
12:32:30.053	Detected the following tools from environment: yarn@3.6.3, nodejs@18.17.1
12:32:30.053	Installing project dependencies: yarn
12:32:30.618	➤ YN0070: Migrating from Yarn 1; automatically enabling the compatibility node-modules linker 👍
12:32:30.619	
12:32:30.658	➤ YN0000: ┌ Resolution step
12:32:30.892	➤ YN0061: │ popper.js@npm:1.16.1 is deprecated: You can find the new Popper v2 at @popperjs/core, this package is dedicated to the legacy v1
12:32:31.456	➤ YN0032: │ fsevents@npm:2.3.3: Implicit dependencies on node-gyp are discouraged
12:32:35.294	➤ YN0000: └ Completed in 4s 635ms
12:32:35.311	➤ YN0000: ┌ Post-resolution validation
12:32:35.311	➤ YN0028: │ The lockfile would have been modified by this install, which is explicitly forbidden.
12:32:35.311	➤ YN0000: └ Completed
12:32:35.311	➤ YN0000: Failed with errors in 4s 654ms
12:32:35.344	Error: Exit with error code: 1
12:32:35.344	    at ChildProcess.<anonymous> (/snapshot/dist/run-build.js)
12:32:35.345	    at Object.onceWrapper (node:events:652:26)
12:32:35.345	    at ChildProcess.emit (node:events:537:28)
12:32:35.345	    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
12:32:35.354	Failed: build command exited with code: 1
12:32:36.612	Failed: error occurred while running build command

対応策

修正するためには、プロジェクトに設置してあるwrangler.tomlファイルにYARN_VERSIONを指定することで解消が可能です
※nodeは固定化したいので記載しています

# wrangler.toml

name = "hogehoge"
pages_build_output_dir = "./build"

[vars]
NODE_VERSION = "18.17.1"
YARN_VERSION = "1.22.19"

本来であれば管理画面から追加することで対応可能なはずですが、build logsを確認してみると値が変わらないことに気がついて、編集してみたところ解決に至りました

参考記事

Team Blog HubをVercelからCloudflare Pagesに移行しました