できない.dev

Next.js の next/image で画像が表示できない

next/image は外部ドメイン画像に images.remotePatterns の許可が必須で、width / height の指定も要る。
ローカル画像は /public 配下に置き絶対パスで参照する。

公開: 更新:

症状できない
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/
500
解決後できた
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/
200

2026-10-13 に node:20(Next.js 15.5.27 / Node.js 20.20.2) で実際に打って取った出力。検証環境の詳細

要約

next/image で画像が表示できないときの原因は、ほぼ 外部ドメインの許可不足・サイズ未指定・パス間違い のいずれかである。
代表的なエラーとして、「Invalid src prop」「hostname is not configured」「Image is missing required width property」が出る。

実行例

外部ホストの画像を images.remotePatterns に登録しないまま next/image で表示すると、Next.js 15.5.27 の開発サーバーは hostname ... is not configured under images を出して 500 を返した。
remotePatterns に images.example.com を足して再起動すると 200 になり、画像は /_next/image 経由の URL に置き換わった。
width と height を渡さない場合も missing required "width" property で 500 になり、両方を渡すと 200 に戻った。

$ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/
500
$ grep -m1 -A1 "Invalid src prop" dev.log
 ⨯ Error: Invalid src prop (https://images.example.com/hero.png) on `next/image`, hostname "images.example.com" is not configured under images in your `next.config.js`
See more info: https://nextjs.org/docs/messages/next-image-unconfigured-host
$ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/
200
$ curl -s http://localhost:3000/ | grep -o 'src="/_next/image[^"]*"'
src="/_next/image?url=https%3A%2F%2Fimages.example.com%2Fhero.png&w=3840&q=75"
$ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/
500
$ grep -m1 -A1 "missing required" dev.log
 ⨯ Error: Image with src "https://images.example.com/hero.png" is missing required "width" property.
    at getImgProps (../../src/shared/lib/get-img-props.ts:484:17)
$ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/
200

— 2026-10-13 時点の出力

検証環境Node.js 20.20.2 / npm 10.8.2 / Python 3.11.2 / 2026-10-13 検証(ほか 2 件)
検証日
実行環境
node:20 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 20.20.2
  • npm 10.8.2
  • Python 3.11.2
  • Git 2.39.5
  • Next.js 15.5.27

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. 外部ドメインを許可する

公式の images 設定ドキュメント(新しいタブで開く) に従い next.config.js を編集する。

module.exports = {
  images: {
    remotePatterns: [
      { protocol: "https", hostname: "images.example.com" },
    ],
  },
};

設定変更後は dev server を必ず再起動する。

2. サイズを必ず指定する

import Image from "next/image";
 
export function Hero() {
  return <Image src="/hero.png" alt="hero" width={1200} height={630} />;
}

サイズが不明な場合は親要素を position: relative にして fill を使う。

<div style={{ position: "relative", width: 320, height: 240 }}>
  <Image src="/hero.png" alt="hero" fill />
</div>

3. 静的エクスポート時の対応

output: 'export' を使うプロジェクトでは、組み込みの画像最適化サーバーが動かない。
images.unoptimized: true を設定するか、Cloudinary などの外部ローダーを設定する。
Image コンポーネントの仕様詳細は公式の <Image> リファレンス(新しいタブで開く) を参照する。

よくある原因

  1. 外部ドメインを許可していない。
    このときは「hostname "images.example.com" is not configured under images」エラーが出る。
  2. サイズを指定していない。
    このときは「Image is missing required width / height property」エラーが出る。
  3. パスの指定を間違えている。
    /public/foo.png を src="./foo.png" のように相対指定している。
  4. 静的エクスポートを使っている。
    output: 'export' では、組み込みの画像最適化を使えない。
  5. CORS や認証で止められている。
    クロスオリジン制限や認証必須 URL で、画像の取得がブロックされている。