Netlify上React项目构建部署失败:缺失ajv模块
React网站Netlify构建失败:找不到ajv模块
问题背景
在Netlify部署的React网站已上线,但修改GitHub文件后无法同步到线上,核心原因是构建阶段持续失败。已尝试将构建命令改为CI= npm run build,问题仍未解决。
错误日志
// 错误日志 8:58:53 PM: Netlify 构建 8:58:54 PM: throw err; 8:58:54 PM: ^ 8:58:54 PM: 错误:找不到模块 'ajv/dist/compile/codegen' 8:58:54 PM: 依赖栈: 8:58:54 PM: - /opt/build/repo/node_modules/ajv-keywords/dist/definitions/typeof.js 8:58:54 PM: - /opt/build/repo/node_modules/ajv-keywords/dist/keywords/typeof.js 8:58:54 PM: - /opt/build/repo/node_modules/ajv-keywords/dist/keywords/index.js 8:58:54 PM: - /opt/build/repo/node_modules/ajv-keywords/dist/index.js 8:58:54 PM: - /opt/build/repo/node_modules/schema-utils/dist/validate.js 8:58:54 PM: - /opt/build/repo/node_modules/schema-utils/dist/index.js 8:58:54 PM: - /opt/build/repo/node_modules/mini-css-extract-plugin/dist/index.js 8:58:54 PM: - /opt/build/repo/node_modules/react-scripts/config/webpack.config.js 8:58:54 PM: - /opt/build/repo/node_modules/react-scripts/scripts/build.js 8:58:54 PM: 在 Module._resolveFilename (node:internal/modules/cjs/loader:1140:15) 8:58:54 PM: 在 Module._load (node:internal/modules/cjs/loader:981:27) 8:58:54 PM: 在 Module.require (node:internal/modules/cjs/loader:1231:19) 8:58:54 PM: 在 require (node:internal/modules/helpers:177:18) 8:58:54 PM: 在 Object.<anonymous> (/opt/build/repo/node_modules/ajv-keywords/dist/definitions/typeof.js:3:19) 8:58:54 PM: 在 Module._compile (node:internal/modules/cjs/loader:1364:14) 8:58:54 PM: 在 Module._extensions..js (node:internal/modules/cjs/loader:1422:10) 8:58:54 PM: 在 Module.load (node:internal/modules/cjs/loader:1203:32) 8:58:54 PM: 在 Module._load (node:internal/modules/cjs/loader:1019:12) 8:58:54 PM: 在 Module.require (node:internal/modules/cjs/loader:1231:19) { 8:58:54 PM: code: 'MODULE_NOT_FOUND', 8:58:54 PM: requireStack: [ 8:58:54 PM: '/opt/build/repo/node_modules/ajv-keywords/dist/definitions/typeof.js', 8:58:54 PM: '/opt/build/repo/node_modules/ajv-keywords/dist/keywords/typeof.js', 8:58:54 PM: '/opt/build/repo/node_modules/ajv-keywords/dist/keywords/index.js', 8:58:54 PM: '/opt/build/repo/node_modules/ajv-keywords/dist/index.js', 8:58:54 PM: '/opt/build/repo/node_modules/schema-utils/dist/validate.js', 8:58:54 PM: '/opt/build/repo/node_modules/schema-utils/dist/index.js', 8:58:54 PM: '/opt/build/repo/node_modules/mini-css-extract-plugin/dist/index.js', 8:58:54 PM: '/opt/build/repo/node_modules/react-scripts/config/webpack.config.js', 8:58:54 PM: '/opt/build/repo/node_modules/react-scripts/scripts/build.js' 8:58:54 PM: ] 8:58:54 PM: } 8:58:54 PM: Node.js v18.20.4 8:58:54 PM: "build.command" 执行失败 8:58:54 PM: 错误信息 8:58:54 PM: 命令执行失败,退出码1: CI= npm run build 8:58:54 PM: 8:58:54 PM: 错误位置 8:58:54 PM: Netlify应用中的构建命令: 8:58:54 PM: CI= npm run build 8:58:54 PM: 解析后的配置 8:58:54 PM: build: 8:58:54 PM: command: CI= npm run build 8:58:54 PM: commandOrigin: ui 8:58:54 PM: environment: 8:58:54 PM: - NPM_FLAGS 8:58:54 PM: publish: /opt/build/repo/build 8:58:54 PM: publishOrigin: ui 8:58:55 PM: 构建阶段失败:构建脚本返回非零退出码2 8:58:55 PM: 构建失败原因:用户错误,构建脚本返回非零退出码2 8:58:55 PM: 构建失败:无法完成网站构建 8:58:55 PM: 构建请求处理完成,耗时45.693秒
解决方案
1. 锁定ajv版本
错误源于ajv-keywords依赖的ajv版本与当前安装版本不兼容,导致找不到指定模块。在package.json的dependencies中添加锁定版本:
"dependencies": { "ajv": "^6.12.6" }
本地执行npm install,提交更新后的package.json和package-lock.json到GitHub,重新触发Netlify构建。
2. 清除Netlify缓存
Netlify可能缓存了旧依赖包引发版本冲突:
- 登录Netlify后台,进入目标网站的Deploys页面
- 点击右上角Trigger deploy,选择Clear cache and deploy site
- 等待构建完成
3. 更新react-scripts
旧版本react-scripts可能存在依赖兼容性问题:
执行npm install react-scripts@latest,提交更新后的依赖文件到GitHub,重新构建。
4. 修改Netlify构建命令
在Netlify后台Site settings > Build & deploy > Build command中,将命令改为:
npm ci && npm run build
npm ci会严格按照package-lock.json安装依赖,避免版本不一致问题。
内容的提问来源于stack exchange,提问作者Faithful
相关产品推荐
相关产品推荐

