Cloudflare PagesにSvelteKitをデプロイする際のNodeバージョン

Cloudflare PagesにSvelteKitをデプロイする際のNodeバージョン

Cloudflare PagesにSvelteKitで作成したアプリケーションをデプロイする際のNodeバージョンでハマってしまったのでちょっとメモ

執筆時(2022/1/24)でのSvelteKitのバージョンは「1.1.1」でCloudflare Pagesにデプロイしようとすると以下のエラーがでてしまいました。

20:20:59.114	npm ERR! code ELIFECYCLE
20:20:59.114	npm ERR! errno 1
20:20:59.114	npm ERR! @sveltejs/kit@1.1.1 postinstall: `node postinstall.js`
20:20:59.114	npm ERR! Exit status 1
20:20:59.114	npm ERR! 
20:20:59.115	npm ERR! Failed at the @sveltejs/kit@1.1.1 postinstall script.
20:20:59.115	npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Node.jsのLTSの最新バージョンは 「18.13.0」だがCloudflare PagesのBuildで利用されるデフォルトのNode.jsのバージョンが「12.18.0」とだいぶ古くインストールに失敗するようです。

参考: Build configuration · Cloudflare Pages docs

ちなみにSvelteKitの要求するNodeバージョンは16.14以上の16.x.xか18.x.xのようです。


  "engines": {
    "node": "^16.14 || >=18"
  },

Cloudflare PagesではNode.jsのバージョンを1.7.xまで指定することができるので16系統の最新を指定すれば、SvelteKitの要求とCloudflare Pageで利用できるバージョンが合致します。

Cloudflare PageではSettingsのEnvironment variablesでNODE_VERSIONの指定が可能ですので、16系統の最新である「v16.19.0」を指定することでビルドとデプロイが正常に動作します。

Cloudflare PagesでNODE_VERSIONの指定を行っている画面のキャプチャ

17系統でもインストール可能という記事を見かけたのですが、試したところ以下のようなエラーで止まってしまいます。

20:43:14.630	npm ERR! engine Not compatible with your version of node/npm: @sveltejs/kit@1.1.1
20:43:14.630	npm ERR! notsup Not compatible with your version of node/npm: @sveltejs/kit@1.1.1
20:43:14.630	npm ERR! notsup Required: {"node":"^16.14 || >=18"}
20:43:14.630	npm ERR! notsup Actual:   {"npm":"8.11.0","node":"v17.9.1"}

SvelteKitは執筆時点でもかなりの速度でアップデートがおこなわれているので一過性の現状の可能性もありますがハマったひとは参考にしてみてください。

スポンサードリンク

«Next.js + TypeScriptでgetStaticPropsの型を定義する方法 | メイン | VSCodeでSveltKitのPrettierのフォーマッティングをエディタ保存時に適用する»