升级至Next JS 13后出现Cannot read properties of undefined (reading 'and')错误
修复Next.js 13升级后
next-transpile-modules导致的构建错误 这个错误的核心原因是**next-transpile-modules与Next.js 13不兼容**,Next.js 13已内置模块转译功能,无需依赖该第三方包。按以下步骤修复:
卸载
next-transpile-modules依赖npm uninstall next-transpile-modules修改
next.config.js配置
将原来基于withTM的配置替换为Next.js 13内置的transpilePackages选项:
原配置示例:const withTM = require('next-transpile-modules')(['package-a', 'package-b']); module.exports = withTM({ // 其他项目配置 });修改后:
/** @type {import('next').NextConfig} */ const nextConfig = { transpilePackages: ['package-a', 'package-b'], // 保留原有其他配置 } module.exports = nextConfig;清理依赖环境并重新安装
- 可选:备份当前
node_modules和package-lock.json,降低操作风险 - 删除旧依赖文件:
Windows命令:
macOS/Linux命令:rmdir /s /q node_modules del package-lock.jsonrm -rf node_modules package-lock.json - 重新安装依赖:
npm install
- 可选:备份当前
验证修复
运行npm run dev或npm run build,确认构建错误消失。
内容的提问来源于stack exchange,提问作者GET
相关产品推荐
相关产品推荐

