Vercelにデプロイしたら‘Error: Could not load the "sharp" module using the linux-x64 runtime’が出た時の対処法
🔨

Vercelにデプロイしたら‘Error: Could not load the "sharp" module using the linux-x64 runtime’が出た時の対処法

Tags
Bug fix
Published
Posted on 2024-09-01
Updated on 2024-09-07
Author
ホッケ肉厚です🐟
このブログはNext.jsをVercelにデプロイして運用しているのですが、メンテナンスしていた際にタイトルのエラーに遭遇しました。 一部今後に負債を残す形で解消したので、備忘録も込みで対処法をシェアします。
 

起きたこと

久々に当ブログのパッケージをメンテしていたら、typescript のアップデート以降以下の条件でAPIリクエスト時にエラーが発生するようになりました。

開発環境

  • npm: v10.8.3
  • Node.js: v20.17.0
  • TypeScript: v5.5.4
  • Next.js: v12.3.1
  • Vercel CLI: v34.1.1
  • sharp: v0.33.5
  • lqip-modern: v2.1.1

エラーログ(APIへのリクエスト時)

Error: Could not load the "sharp" module using the linux-x64 runtime Possible solutions: - Ensure optional dependencies can be installed: npm install --include=optional sharp - Ensure your package manager supports multi-platform installation: See https://sharp.pixelplumbing.com/install#cross-platform - Add platform-specific dependencies: npm install --os=linux --cpu=x64 sharp - Consult the installation documentation: See https://sharp.pixelplumbing.com/install at Object.<anonymous> (/var/task/node_modules/lqip-modern/node_modules/sharp/lib/sharp.js:113:9) at Module._compile (node:internal/modules/cjs/loader:1358:14) at Module._extensions..js (node:internal/modules/cjs/loader:1416:10) at Module.load (node:internal/modules/cjs/loader:1208:32) at Module._load (node:internal/modules/cjs/loader:1024:12) at /opt/rust/nodejs.js:1:11546 at Function.cr (/opt/rust/nodejs.js:1:11920) at e.<computed>.re._load (/opt/rust/nodejs.js:1:11516) at Module.require (node:internal/modules/cjs/loader:1233:19) at require (node:internal/modules/helpers:179:18)
このブログでは画像編集ライブラリのsharp@0.33.5を使用しているのですが、Vercel上のruntimeでは上手く動いていないようでした。

やったこと

一旦エラーログに記載のos依存設定込みのパッケージインストールを試しましたが、効果なし…
$ npm install --include=optional sharp $ npm install --os=linux --cpu=x64 sharp
調べてみると、sharpの最新バージョンはまだこの問題に対応していないらしく、ひとまずv0.32.6にダウングレードしたら動いたよ、との報告が多数ありました。
Vercel only: 0.33.0: error Could not load the "sharp" module using the linux-x64 runtime
Updated Jan 10, 2024
恒久対応にあんまり時間をかけたくないので、今回はダウングレードすることに…
$ npm install sharp@0.32.6
しかし、再度デプロイしてもエラーが改善されませんでした。 もう少し追ってみると、ローディング時の画像ぼかし用に使っているlqip-modern@2.1.1sharp@0.33.4を使っているようで、これが原因でした。
"node_modules/lqip-modern": { "version": "2.1.0", : "dependencies": { "sharp": "^0.33.4", <- エラーが出るバージョンのまま : }, : }
package-lock.json
ということで、lqip-modernを一旦v2.0.0固定にしてエラー解消出来ました👏
"dependencies": { "lqip-modern": "2.0.0", : }
package.json
ちなみに、つい最近lqip-modern@2.0.1sharpのバージョンアップ対応してたみたい👀 https://github.com/transitive-bullshit/lqip-modern/releases/tag/v2.1.0

まとめ

lqip-modernがバージョン固定されてしまうので暫定対応にはなりますが、sharp起因のVercelデプロイ時エラーの解消法でした。
パッケージの依存関係解消はあっちを見てこっちを見て…の繰り返しで好きではないのですが、時間をかけずに解決出来る良い方法はないものか🤔
Lambdaユーザーも同様のエラーを報告していたので影響大きそうだし、早めに改善してもらいたい🤮