NextJS项目本地正常但Vercel部署报错:找不到resolve-href.js模块
部署Next.js应用到Vercel时出现MODULE_NOT_FOUND错误
我第一次用NextJS框架构建React应用,本地执行npm run build或npm run dev都能正常运行,但部署到Vercel时出错,错误日志如下:
[12:51:06.189] Running build in Washington, D.C., USA (East) – iad1 [12:51:06.234] Cloning github.com/tym1226/nextjs-personal-blog (Branch: master, Commit: 2d9acfd) [12:51:06.542] Previous build cache not available [12:51:11.849] Cloning completed: 5.614s [12:51:13.255] Running "vercel build" [12:51:13.827] Vercel CLI 28.16.12 [12:51:14.166] Installing dependencies... [12:51:16.299] [12:51:16.299] added 2 packages in 2s [12:51:16.299] [12:51:16.299] 66 packages are looking for funding [12:51:16.300] run `npm fund` for details [12:51:16.315] Detected Next.js version: 12.3.4 [12:51:16.317] Running "CI=' ' npm run build" [12:51:16.699] [12:51:16.699] > nextjs-course@0.1.0 build [12:51:16.699] > next build [12:51:16.699] [12:51:17.324] Attention: Next.js now collects completely anonymous telemetry regarding usage. [12:51:17.324] This information is used to shape Next.js' roadmap and prioritize features. [12:51:17.325] You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL: [12:51:17.325] https://nextjs.org/telemetry [12:51:17.325] [12:51:17.490] info - Linting and checking validity of types... [12:51:17.676] info - Creating an optimized production build... [12:51:29.592] info - Compiled successfully [12:51:29.606] info - Collecting page data... [12:51:29.975] unhandledRejection Error: Cannot find module 'next/dist/shared/lib/router/utils/resolve-href.js' [12:51:29.976] Require stack: [12:51:29.976] - /vercel/path0/.next/server/pages/_app.js [12:51:29.976] - /usr/local/share/.config/yarn/global/node_modules/next/dist/server/require.js [12:51:29.977] - /usr/local/share/.config/yarn/global/node_modules/next/dist/server/load-components.js [12:51:29.977] - /usr/local/share/.config/yarn/global/node_modules/next/dist/export/worker.js [12:51:29.977] - /usr/local/share/.config/yarn/global/node_modules/next/dist/build/worker.js [12:51:29.978] - /usr/local/share/.config/yarn/global/node_modules/next/dist/compiled/jest-worker/processChild.js [12:51:29.978] at Module._resolveFilename (node:internal/modules/cjs/loader:1039:15) [12:51:29.978] at mod._resolveFilename (/usr/local/share/.config/yarn/global/node_modules/next/dist/build/webpack/require-hook.js:23:32) [12:51:29.979] at Module._load (node:internal/modules/cjs/loader:885:27) [12:51:29.979] at Module.require (node:internal/modules/cjs/loader:1105:19) [12:51:29.979] at require (node:internal/modules/cjs/helpers:103:18) [12:51:29.980] at 7782 (/vercel/path0/.next/server/pages/_app.js:285:18) [12:51:29.980] at __webpack_require__ (/vercel/path0/.next/server/webpack-runtime.js:25:42) [12:51:29.980] at 9170 (/vercel/path0/.next/server/chunks/515.js:97:20) [12:51:29.981] at __webpack_require__ (/vercel/path0/.next/server/webpack-runtime.js:25:42) [12:51:29.981] at 1515 (/vercel/path0/.next/server/chunks/515.js:560:18) { [12:51:29.982] type: 'Error', [12:51:29.982] code: 'MODULE_NOT_FOUND', [12:51:29.982] requireStack: [ [12:51:29.983] '/vercel/path0/.next/server/pages/_app.js', [12:51:29.983] '/usr/local/share/.config/yarn/global/node_modules/next/dist/server/require.js', [12:51:29.983] '/usr/local/share/.config/yarn/global/node_modules/next/dist/server/load-components.js', [12:51:29.984] '/usr/local/share/.config/yarn/global/node_modules/next/dist/export/worker.js', [12:51:29.985] '/usr/local/share/.config/yarn/global/node_modules/next/dist/build/worker.js', [12:51:29.985] '/usr/local/share/.config/yarn/global/node_modules/next/dist/compiled/jest-worker/processChild.js' [12:51:29.986] ] [12:51:29.986] } [12:51:30.046] Error: Command "CI=' ' npm run build" exited with 1
我搜了相关问题,试了下面两种方法,但还是报错:
- 删除.next文件夹或node_modules文件夹
- 修改构建命令为
CI='' npm run build
可能的解决办法
锁定Next.js版本并重新安装依赖
在package.json中明确指定Next.js版本为12.3.4(和本地运行版本一致),避免Vercel自动安装不同版本。然后执行以下步骤:- 运行
npm cache clean --force清除npm缓存 - 删除
package-lock.json、node_modules和.next文件夹 - 重新执行
npm install,将更新后的package-lock.json提交到仓库后重新部署
- 运行
检查代码中是否引用了Next.js内部私有模块
错误提示找不到next/dist/shared/lib/router/utils/resolve-href.js,这是Next.js的内部私有模块,不应该被直接引用。检查项目代码或依赖,把这类引用替换成官方公开的API(比如路由操作改用next/router提供的方法)。禁用Vercel构建缓存
进入Vercel项目的设置页面,找到构建相关选项,关闭构建缓存功能,强制Vercel重新安装所有依赖并构建项目。切换包管理器
如果当前用npm构建有问题,可以尝试改用yarn:在项目根目录生成yarn.lock文件,然后在Vercel设置中将构建命令改为yarn install && yarn build后重新部署。
内容的提问来源于stack exchange,提问作者Emms555_t
相关产品推荐
相关产品推荐

