已配置ignoreBuildErrors:true,Next.js构建仍未跳过类型检查?
Next.js构建时已设置
ignoreBuildErrors仍触发类型检查导致失败的解决办法 版本信息
- Node.js: 16.20.2
- TypeScript: 4.5.2
- Next.js: 13.4.19
- @types/react: 17.0.13
next.config.js配置
const withPWA = require("next-pwa"); const runtimeCaching = require("next-pwa/cache"); const { i18n } = require("./next-i18next.config"); module.exports = withPWA({ i18n, pwa: { disable: process.env.NODE_ENV === "development", dest: "public", runtimeCaching, }, async redirects() { return [ { source: "/", destination: "/home ", permanent: false, }, ]; }, images: { domains: [ "maps.googleapis.com", "googleusercontent.com", "graph.facebook.com", "res.cloudinary.com", "s3.amazonaws.com", "127.0.0.1", ], }, typescript: { ignoreBuildErrors: true, }, });
tsconfig.json配置
{ "compilerOptions": { "baseUrl": ".", "target": "es5", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "alwaysStrict": true, "noFallthroughCasesInSwitch": true, "noUnusedLocals": true, "noUnusedParameters": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "downlevelIteration": true, "jsx": "preserve", "paths": { "@assets/": ["src/assets/"], "@components/": ["src/components/"], "@contexts/": ["src/contexts/"], "@data/": ["src/data/"], "@settings/": ["src/settings/"], "@ts-types/": ["src/ts-types/"], "@utils/": ["src/utils/"] } }, "include": ["next-env.d.ts", "src/*/.ts", "src/*/.tsx", "src/*/.js", "src/data/user", "src/pages/jee_mains_paper/student_login_form.tsx"], "exclude": ["node_modules"] }
构建错误信息
yarn build yarn run v1.22.5 $ next build info - Loaded env from /var/www/complete-project/admin/.env.local info - Loaded env from /var/www/complete-project/admin/.env info - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5 Failed to compile. ./src/components/all-shop-visitors/all-shop-visitor-list.tsx:4:1 Type error: 'ROUTES' is declared but its value is never read. 2 | import { Table } from "@components/ui/table"; 3 | import ActionButtons from "@components/common/action-buttons"; >4 | import { ROUTES } from "@utils/routes"; | ^ 5 | import { CategoryPaginator } from "@ts-types/generated"; 6 | import { useTranslation } from "next-i18next"; 7 | import { useIsRTL } from "@utils/locals"; error Command failed with exit code 1.
解决办法
1. 排查next.config.js配置传递问题
确认withPWA插件是否正确传递了TypeScript配置。可以临时移除withPWA包装,直接导出配置测试是否能跳过类型检查:
// 临时修改next.config.js const { i18n } = require("./next-i18next.config"); module.exports = { i18n, // ...其他配置 typescript: { ignoreBuildErrors: true, }, };
如果移除插件后生效,说明withPWA可能覆盖了配置,需要检查插件文档或更新插件版本。
2. 调整TypeScript编译规则
当前错误来自noUnusedLocals和noUnusedParameters的严格检查,有三种处理方式:
- 全局关闭规则:在
tsconfig.json中将这两个选项设为false:"noUnusedLocals": false, "noUnusedParameters": false - 单个文件禁用:在报错文件顶部添加注释忽略该规则:
// @ts-ignore: unused import import { ROUTES } from "@utils/routes"; - 直接修复:删除未使用的
ROUTES导入语句。
3. 版本兼容性调整
Next.js 13.4.x推荐搭配TypeScript 4.9+版本,同时@types/react版本需要与Next.js内置的React版本(18.x)匹配,建议:
- 升级TypeScript到
4.9.x或5.x - 升级@types/react到
18.x版本
4. 清理缓存
清理Next.js构建缓存和Node模块,重新安装依赖:
rm -rf .next node_modules yarn install yarn build
内容的提问来源于stack exchange,提问作者vInEnDeRsInGh
相关产品推荐
相关产品推荐

