Closed10

standalone ビルドした Next.js アプリケーションで `sharp` に関するエラーに対応したログ

snakasnaka

standalone ビルドした Next.js アプリケーションのコンテナで以下のエラーがログに出ていた

 ⨯ Error: 'sharp' is required to be installed in standalone mode for the image optimization to function correctly. Read more at: https://nextjs.org/docs/messages/sharp-missing-in-production

standalone ビルドする前は Warning だったものが、standalone ビルドでは sharp が必須になった。

snakasnaka

ローカル環境で require してみる

REPL (node) で require 試してみると以下のエラー

> sharp = require('sharp')
Uncaught Error: Could not load the "sharp" module using the linux-x64 runtime
Possible solutions:
- Add platform-specific dependencies:
    npm install --os=linux --cpu=x64 sharp
  or
    npm install --force @img/sharp-linux-x64
snakasnaka

npm install --os=linux --cpu=x64 sharp を追加してみる

該当アプリケーションは ECS 上でコンテナとして実行されている。
Dockerfile に以下の行を追加した。

RUN npm install -g --os=linux --cpu=x64 sharp

これで動くようになった。

snakasnaka

--os=linux --cpu=x64 sharp は必須なのか?

久しぶりに見直しているが、 require したときのエラーメッセージも変わっていた。

> sharp = require('sharp')
Uncaught Error: Could not load the "sharp" module using the linuxmusl-x64 runtime
Possible solutions:
- Ensure optional dependencies can be installed:
    npm install --include=optional sharp
    yarn add sharp --ignore-engines
- Ensure your package manager supports multi-platform installation:
    See https://sharp.pixelplumbing.com/install#cross-platform
- Add platform-specific dependencies:
    npm install --os=linux --libc=musl --cpu=x64 sharp
- Consult the installation documentation:
    See https://sharp.pixelplumbing.com/install
snakasnaka

現状プロジェクトで利用しているパッケージマネージャが Yarn v1 であるため、これは仕方なさそう。という結論に至った。

このスクラップは4ヶ月前にクローズされました