Cloudflare Pages で指定した Node.js のバージョンが利用できず Error: Exit with error code: 126

Cloudflare Pages で .tool-versions にて指定した Node.js のバージョンが存在せず Error: Exit with error code: 126 になったときの解決方法をご紹介します。

背景 Cloudflare Pages で Node.js LTS 最新版を利用したらエラー

Cloudflare Pages で Node.js LTS 最新版を利用したら以下のようなエラーログが出力されて deploy できませんでした。

2023-10-07T13:07:04.419035Z	Detected the following tools from environment: [email protected], [email protected]
2023-10-07T13:07:04.419523Z	Installing project dependencies: yarn
2023-10-07T13:07:04.827668Z	No preset version installed for command yarn
2023-10-07T13:07:04.828016Z	Please install a version by running one of the following:
2023-10-07T13:07:04.828213Z	
2023-10-07T13:07:04.828389Z	asdf install nodejs 18.18.0
2023-10-07T13:07:04.828545Z	
2023-10-07T13:07:04.828697Z	or add one of the following versions in your config file at /opt/buildhome/repo/.tool-versions
2023-10-07T13:07:04.839247Z	nodejs 14.21.3
2023-10-07T13:07:04.839524Z	nodejs 16.20.2
2023-10-07T13:07:04.839682Z	nodejs 18.17.1
2023-10-07T13:07:04.839851Z	nodejs 20.5.1
2023-10-07T13:07:04.844692Z	Error: Exit with error code: 126
2023-10-07T13:07:04.845006Z	    at ChildProcess. (/snapshot/dist/run-build.js)
2023-10-07T13:07:04.845215Z	    at Object.onceWrapper (node:events:652:26)
2023-10-07T13:07:04.845436Z	    at ChildProcess.emit (node:events:537:28)
2023-10-07T13:07:04.845607Z	    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
2023-10-07T13:07:04.854278Z	Failed: build command exited with code: 1
2023-10-07T13:07:05.812512Z	Failed: error occurred while running build command

解決方法 Cloudflare Pages が対応している Node.js バージョンをインストールする

Cloudflare Pages が対応している Node.js バージョンをインストールすることで解決しました。

diff --git a/.tool-versions b/.tool-versions
--- a/.tool-versions
+++ b/.tool-versions
@@ -1 +1 @@
-nodejs 18.18.0
+nodejs 18.17.1

以上、Cloudflare Pages で .tool-versions に指定した Node.js のバージョンが利用できず Error: Exit with error code: 126 になって焦った、現場からお送りしました。