Next.js执行yarn dev时出现webpack缓存失败错误,求解决方案
错误信息:
macbookpro@macs-MacBook-Pro-2 nextjs-nftmarketplace % yarn dev
yarn run v1.22.15
$ next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info - SWC minify release candidate enabled. https://nextjs.link/swcmin
event - compiled client and server successfully in 2.2s (169 modules)
wait - compiling...
event - compiled successfully in 49 ms (142 modules)
wait - compiling...
event - compiled successfully in 20 ms (27 modules)[webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Unable to snapshot resolve dependencies [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Unable to snapshot resolve dependencies
解决方案:
清理缓存并重装依赖
执行以下命令:yarn cache clean rm -rf .next node_modules/.cache yarn install yarn dev禁用webpack文件系统缓存
在项目根目录的next.config.js中添加如下配置:module.exports = { webpack: (config) => { config.cache = false; return config; }, };注:禁用缓存会小幅降低编译速度,但可直接绕过缓存快照问题。
排查依赖冲突
执行yarn list查看依赖树,检查是否存在重复或版本不兼容的webpack相关依赖。若有冲突,可在package.json中添加resolutions字段强制统一版本,示例:"resolutions": { "webpack": "^5.88.2" }添加后重新执行
yarn install。切换包管理器测试
删除yarn.lock和node_modules,改用npm尝试:rm -rf node_modules yarn.lock npm install npm run dev更新Next.js到稳定版本
当前日志显示启用了SWC minify候选版本,可能存在兼容性问题,更新到最新稳定版:yarn add next@latest yarn dev
内容的提问来源于stack exchange,提问作者Mora Lynn

