Next.js+Sanity.io项目Vercel生产环境部署失败求助
问题:Next.js + Sanity.io 生产部署失败,提示找不到
sanity/cli 模块 我有一个 Next.js + Sanity.io 项目,本地执行 vercel build 后用 vercel deploy --prebuilt 预构建部署一切正常,但运行 vercel --prod 部署到生产环境时持续失败,报错日志如下:
[16:48:49.455] Previous build cache not available [16:48:49.684] Downloading 69 deployment files... [16:48:50.586] Running "vercel build" [16:48:51.246] Vercel CLI 28.10.0 [16:48:51.661] Installing dependencies... [16:48:52.107] yarn install v1.22.17 [16:48:52.183] [1/4] Resolving packages... [16:48:52.393] [2/4] Fetching packages... [16:49:16.863] [3/4] Linking dependencies... [16:49:31.972] [4/4] Building fresh packages... [16:49:32.468] Done in 40.37s. [16:49:32.503] Detected Next.js version: 13.1.1-canary.1 [16:49:32.505] Running "yarn run build" [16:49:32.842] yarn run v1.22.17 [16:49:32.888] $ next build [16:49:33.425] Attention: Next.js now collects completely anonymous telemetry regarding usage. [16:49:33.425] This information is used to shape Next.js' roadmap and prioritize features. [16:49:33.425] 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: [16:49:33.425] https://nextjs.org/telemetry [16:49:33.426] [16:49:33.588] info - Linting and checking validity of types... [16:49:38.211] Failed to compile. [16:49:38.212] [16:49:38.212] ./sanity/sanity.cli.ts:1:31 [16:49:38.212] Type error: Cannot find module 'sanity/cli' or its corresponding type declarations. [16:49:38.212] [16:49:38.213] [0m[31m[1m>[22m[39m[90m 1 | [39m[36mimport[39m {defineCliConfig} [36mfrom[39m [32m'sanity/cli'[39m[0m [16:49:38.213] [0m [90m | [39m [31m[1m^[22m[39m[0m [16:49:38.213] [0m [90m 2 | [39m[0m [16:49:38.213] [0m [90m 3 | [39m[36mexport[39m [36mdefault[39m defineCliConfig({[0m [16:49:38.213] [0m [90m 4 | [39m api[33m:[39m {[0m [16:49:38.253] error Command failed with exit code 1. [16:49:38.254] info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. [16:49:38.283] Error: Command "yarn run build" exited with 1
项目根目录package.json:
{ "name": "portfolio-project", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@portabletext/react": "^2.0.0", "@sanity/image-url": "^1.0.1", "@types/node": "18.11.12", "@types/react": "18.0.26", "@types/react-dom": "18.0.9", "@vercel/analytics": "^0.1.6", "eslint": "8.29.0", "eslint-config-next": "13.0.6", "framer-motion": "^7.6.19", "next": "13.0.6", "next-sanity": "^3.1.5", "react": "18.2.0", "react-dom": "18.2.0", "react-simple-typewriter": "^5.0.1", "react-social-icons": "^5.15.0", "typescript": "4.9.4" }, "devDependencies": { "autoprefixer": "^10.4.13", "postcss": "^8.4.19", "tailwind-scrollbar": "^2.0.1", "tailwindcss": "^3.2.4" } }
sanity文件夹下的package.json:
{ "name": "portfolio-project", "private": true, "version": "1.0.0", "main": "package.json", "license": "UNLICENSED", "scripts": { "dev": "sanity dev", "start": "sanity start", "build": "sanity build", "deploy": "sanity deploy", "deploy-graphql": "sanity graphql deploy" }, "keywords": [ "sanity" ], "dependencies": { "@sanity/vision": "^3.0.0", "react": "^18.2.0", "react-dom": "^18.2.0", "react-is": "^18.2.0", "sanity": "^3.0.0", "styled-components": "^5.2.0" }, "devDependencies": { "@sanity/eslint-config-studio": "^2.0.1", "eslint": "^8.6.0", "prettier": "^2.8.1", "typescript": "^4.0.0" }, "prettier": { "semi": false, "printWidth": 100, "bracketSpacing": false, "singleQuote": true } }
我已经尝试过升级Next.js到canary版本、在本地安装@sanity/cli等方法,但都没有解决问题,求可行的解决思路。
解决思路
1. 核心原因
生产部署时,Vercel 在根目录执行next build,Next.js的类型检查会扫描项目内所有TS文件,包括sanity/目录下的sanity.cli.ts,但根目录package.json未安装sanity依赖(仅子目录安装),因此触发模块找不到的错误。
2. 具体解决方案
方案一:让Next.js忽略Sanity目录
在项目根目录创建/修改next.config.js,配置Next.js跳过Sanity目录的编译和类型检查:
/** @type {import('next').NextConfig} */ const nextConfig = { typescript: { exclude: ['sanity/**/*'] }, eslint: { dirs: ['src'] // 仅检查业务代码目录,比如src } } module.exports = nextConfig
方案二:修改根目录TS配置
在根目录tsconfig.json中添加exclude字段,排除Sanity目录:
{ "compilerOptions": { // 原有配置 }, "exclude": ["node_modules", "sanity"] }
方案三(临时应急):在根目录安装Sanity依赖
不推荐长期使用(会增加根目录依赖体积),但可快速验证问题:
yarn add sanity --dev
3. 验证步骤
修改配置后,先在本地执行next build测试,确认本地构建成功后,再运行vercel --prod部署即可。
内容的提问来源于stack exchange,提问作者JL.
相关产品推荐
相关产品推荐

