🐙

Cloudflare Pages で node.js のバージョンエラーでビルドできなくなった話

に公開

2ヶ月前頃から放置していた自分の Cloudflare Pages のプロジェクトをアップデートしようとしたところ、以下のエラーが出てビルドに失敗するようになりました。

10:55:35.542	asdf install nodejs 23.6.0
10:55:35.542	
10:55:35.542	or add one of the following versions in your config file at /opt/buildhome/repo/.tool-versions
10:55:35.549	nodejs 14.21.3
10:55:35.550	nodejs 16.20.2
10:55:35.550	nodejs 18.17.1
10:55:35.550	nodejs 20.19.0
10:55:35.550	nodejs 22.16.0
10:55:35.556	Error: Exit with error code: 126
10:55:35.556	    at ChildProcess.<anonymous> (/snapshot/dist/run-build.js)
10:55:35.556	    at Object.onceWrapper (node:events:652:26)
10:55:35.556	    at ChildProcess.emit (node:events:537:28)
10:55:35.556	    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
10:55:35.563	Failed: build command exited with code: 1
10:55:36.780	Failed: error occurred while running build command

結論から言うとこのエラーは .tool-version に記載しているバージョンが Cloudflare Pages の環境に対応していないバージョンなのでエラーになっています。

.tool-version を確認したところ nodejs 23.6.0 の記載がありました。
このファイルは自分で直接作ったことはなかったです。
公式の手順に従ってプロジェクト作成したときに自動で作られたものなので、今回のエラーが出るまで意識したことはありませんでした。

.tool-version を以下のように変更した結果、ビルドに成功しました!

- nodejs 23.6.0
+ nodejs 22.16.0

Cloudflare Pages と Cloudflare workers が統合する動きがあるので、それによって使える環境の制約も変化しているみたいです。

Cloudflare workers への移行についても検討したほうが良いと思いますが、Cloudflare Pages として動かしたいだけならこの変更だけでできるようなので移行については別途タイミングを検討すれば良いかなとなりました。

Discussion