Vercel部署Next.js项目报错:找不到pages目录,本地运行正常求助
解决Vercel部署Next.js时提示“Couldn't find a
pages directory”的问题 我尝试将Next.js项目上传至Vercel托管,但部署时出现如下报错:
Error: > Couldn't find a `pages` directory. Please create one under the project root at Object.findPagesDir (/vercel/path0/node_modules/next/dist/lib/find-pages-dir.js:39:15) at /vercel/path0/node_modules/next/dist/build/index.js:92:77 at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/trace/trace.js:79:20) at async Object.build [as default] (/vercel/path0/node_modules/next/dist/build/index.js:58:29) error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. Error: Command "yarn run build" exited with 1项目在本地运行正常,项目文件目录截图如下:
从目录截图能看到,你的pages目录放在src文件夹下,本地能正常运行是因为Next.js原生支持src/pages结构,但Vercel部署时未正确识别该结构,按以下方案解决:
方案1:配置next.config.js指定pages目录
在项目根目录新建或修改next.config.js,添加目录配置:module.exports = { dir: 'src', pageExtensions: ['js', 'jsx', 'ts', 'tsx'], }这段配置会明确告诉Next.js从
src目录下查找pages文件夹。方案2:调整Vercel部署设置
- 登录Vercel控制台打开你的项目
- 进入「Settings」→「Build & Development Settings」
- 确认「Root Directory」设为项目根目录(留空或填
.均可) - 也可以修改「Build Command」,手动指定pages目录:
yarn run build -- --dir src
方案3:检查.gitignore是否排除了src目录
打开项目根目录的.gitignore文件,确认没有src相关的排除规则,如果有就删掉对应行,然后重新提交代码到仓库,再触发Vercel重新部署。
内容的提问来源于stack exchange,提问作者Rainysky
相关产品推荐
相关产品推荐


