You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js Image组件src属性无效报错:已配置域名仍提示未配置

解决Next.js Image组件提示hostname未配置的问题
  • 重启开发服务器
    修改next.config.js后必须重启服务,否则新的图片域名配置不会生效。停止当前运行的开发服务,重新执行npm run dev或yarn dev。

  • 修正remotePatterns的pathname格式
    你的配置里pathname缺少开头的/,Next.js要求remotePatterns的pathname必须以/开头才能正确匹配所有子路径。修改后的配置如下:

    const nextConfig = {
      reactStrictMode: true,
      swcMinify: true,
      optimizeFonts: true,
      images: {
        remotePatterns: [
          {
            protocol: 'https',
            hostname: 'img-b.udemycdn.com',
            pathname: '/**', // 添加开头的斜杠
          },
        ],
      },
    };
    
    module.exports = nextConfig;
    
  • 适配旧版Next.js(版本<12.3.0)
    如果你的Next.js版本低于12.3.0,remotePatterns特性还未推出,需要改用domains配置:

    const nextConfig = {
      reactStrictMode: true,
      swcMinify: true,
      optimizeFonts: true,
      images: {
        domains: ['img-b.udemycdn.com'],
      },
    };
    
    module.exports = nextConfig;
    

内容的提问来源于stack exchange,提问作者Mohammad Abbass

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 02:19:53