本地运行Next.js 14.0.3时遇语法错误及Hydration问题求助
Next.js 14.0.3 本地运行报错:SyntaxError、ChunkLoadError 及 Hydration 错误
运行Next.js 14.0.3项目时出现以下问题:
- 控制台抛出
Uncaught SyntaxError: Invalid or unexpected token,错误指向localhost:3000/_next/static/c…s/app/layout.js:436 - 超时后触发
ChunkLoadError: Loading chunk app/layout failed,同时出现Hydration警告:Warning: An error occurred during hydration. The server HTML was replaced with client content in <#document> - 注释RootLayout内所有代码仅保留最基础结构后,问题依然存在
错误日志详情
SyntaxError 日志
Uncaught SyntaxError: Invalid or unexpected token :3000/_next/static/c…s/app/layout.js:436
ChunkLoadError 日志
Unhandled Runtime Error ChunkLoadError: Loading chunk app/layout failed. (timeout: http://localhost:3000/_next/static/chunks/app/layout.js)
调用栈
__webpack_require__.f.j file:///C:/Users/atw0502d/source/repos/mysite/site/.next/static/chunks/webpack.js (853:29) <unknown> file:///C:/Users/atw0502d/source/repos/mysite/site/.next/static/chunks/webpack.js (155:40) Array.reduce <anonymous> __webpack_require__.e file:///C:/Users/atw0502d/source/repos/mysite/site/.next/static/chunks/webpack.js (154:67) fn.e file:///C:/Users/atw0502d/source/repos/mysite/site/.next/static/chunks/webpack.js (391:50) loadChunk node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-client.browser.development.js (202:31) preloadModule node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-client.browser.development.js (128:0) resolveModule node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-client.browser.development.js (1756:0) processFullRow node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-client.browser.development.js (1827:0) processBinaryChunk node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-client.browser.development.js (1975:0) progress node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-client.browser.development.js (2055:0)
Hydration 警告
Warning: An error occurred during hydration. The server HTML was replaced with client content in <#document>
RootLayout 代码
import './globals.css'; import type { Metadata } from 'next'; import { Inter } from 'next/font/google'; import Header from './header/page'; import Footer from './footer/page'; import Link from 'next/link'; import styles from './Styles.module.css'; import { Suspense } from 'react'; import { Skeleton } from '@mui/material'; const inter = Inter({ subsets: ['latin'] }); export const metadata: Metadata = { title: 'Site ', description: 'My Site', }; export default function RootLayout({ children, }: { children: React.ReactNode; }) { return ( <html lang="en" id="page-container"> <body className={`${inter.className} `}> <div className="w-screen bg-white"> <Suspense fallback={<Skeleton width={1500} height={200}></Skeleton>}> <Link href="#main" className={styles.stmcl} style={{ width: '300px' }} > Skip To Content </Link> </Suspense> </div> <div> <div className="bg-white" style={{ display: 'block' }}> <Suspense fallback={<Skeleton width={1500} height={200}></Skeleton>} > <Header /> </Suspense> <main id="content" className="min-h-[40em] w-full"> <Suspense fallback={<Skeleton width={1500} height={200}></Skeleton>} > {children} </Suspense> </main> </div> <Suspense fallback={<Skeleton width={1500} height={200}></Skeleton>}> <Footer /> </Suspense> </div> </body> </html> ); }
解决方案
1. 清理缓存与构建产物
执行命令彻底清理旧文件并重启服务:
# 删除.next构建目录 rm -rf .next # 可选:删除依赖和锁文件(若怀疑依赖异常) rm -rf node_modules package-lock.json # 重新安装依赖 npm install # 重启开发服务 npm run dev
2. 检查文件编码
SyntaxError常因文件编码异常导致,确认app/layout.tsx(或.js)及相关文件为UTF-8无BOM格式:
- 用VS Code打开文件,右下角查看编码,若显示UTF-8 with BOM,切换为UTF-8并保存
- 同步检查
globals.css、组件文件等是否存在编码问题
3. 排查依赖兼容性
Next.js 14.0.3与部分第三方库可能存在冲突:
- 升级
@mui/material到最新稳定版,确保和Next.js 14兼容 - 执行
npm list next确认项目依赖的Next.js版本为14.0.3,无嵌套版本冲突
4. 临时关闭React Strict Mode
在next.config.js中关闭Strict Mode,排查是否为Strict Mode下的 hydration 问题:
/** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: false, }; module.exports = nextConfig;
5. 更换开发端口
端口占用或缓存可能导致资源加载异常,指定其他端口启动:
npm run dev -- -p 3001
6. 恢复默认webpack配置
若项目自定义了webpack配置,移除自定义规则后测试,确认是否因chunk拆分规则异常导致问题
内容的提问来源于stack exchange,提问作者Before The Empire
相关产品推荐
相关产品推荐

