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

Next.js执行yarn dev时出现webpack缓存失败错误,求解决方案

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 17:01:36