使用Next.js Image组件时出现TypeError运行时错误求助
解决Next.js Image组件的
TypeError: Cannot read properties of undefined (reading 'call')错误 以下是针对该问题的排查和解决步骤:
1. 修复依赖版本冲突
该错误常因Next.js、React或React-dom版本不匹配导致,执行以下命令更新到最新兼容版本:
npm install next@latest react@latest react-dom@latest
2. 检查图片参数与路径
- 确认
width和height设置合理:你当前设置的width={50}、height={10}可能与图片实际宽高比差异过大,建议先替换为图片原始宽高值测试。 - 验证图片路径:确认
../assets/IMG-20221115-WA0001.jpg存在于对应目录,注意Linux/macOS环境下文件名区分大小写。
3. 简化Next.js配置
若你的next.config.js有自定义配置,暂时替换为基础配置测试是否是配置冲突:
/** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, } module.exports = nextConfig
4. 清除缓存重启服务
执行以下命令清除缓存并重新构建:
rm -rf node_modules .next package-lock.json npm install npm run dev
5. 验证组件导出方式
尝试将默认导出改为命名导出测试:
// Header组件内修改导出方式 export function Header() { return ( <header> <div> <div> <Image src={myImage} width={50} height={10} alt="Logo" /> </div> </div> </header> ) }
导入时改为:
import { Header } from './Header'
内容的提问来源于stack exchange,提问作者Karan Raghwani
相关产品推荐
相关产品推荐

