CircleCI配置Udacity项目CI/CD遇npm构建及依赖冲突问题求助
为Udacity非自有项目搭建CircleCI CI/CD时的npm构建错误
背景
需要为Udacity项目(仓库地址:https://github.com/udacity/cdond-c3-projectstarter)搭建CI/CD流程,不得修改项目代码,已编写CircleCI的config.yml配置文件如下:
jobs: build-frontend: docker: - image: cimg/node:13.8.0 steps: - checkout - restore_cache: keys: [frontend-build] - run: name: Build front-end command: | cd frontend npm install npm audit fix --force npm run build exit 1 - save_cache: paths: [frontend/node_modules] key: frontend-build build-backend: docker: - image: cimg/node:13.8.0 steps: - checkout - restore_cache: keys: [backend-build] - run: name: Back-end build command: | cd backend npm install npm audit fix --force npm run build exit 1 - save_cache: paths: [backend/node_modules] key: backend-build
但执行时持续遇到三类npm错误:
1. 前端构建错误
npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! react-native-template-redux-typescript@3.4.0 build: webpack --mode development --progress --colors npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the react-native-template-redux-typescript@3.4.0 build script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /home/circleci/.npm/_logs/2022-11-14T18_06_06_579Z-debug.log Exited with code exit status 1 CircleCI received exit code 1
2. 后端构建错误
npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! glee2@1.0.0 build: `tsc` npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the glee2@1.0.0 build script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /home/circleci/.npm/_logs/2022-11-14T19_21_45_828Z-debug.log Exited with code exit status 2 CircleCI received exit code 2
3. 依赖解析冲突错误
error code ERESOLVE 122 error ERESOLVE could not resolve 123 error 124 error While resolving: [1mglee2[22m@[1m1.0.0[22m 124 error Found: [1m@nestjs/cqrs[22m@[1m9.0.1[22m[2m[22m 124 error [2mnode_modules/@nestjs/cqrs[22m 124 error [1m@nestjs/cqrs[22m@"[1m^6.1.0[22m" from the root project 124 error 124 error Could not resolve dependency: 124 error [1m@nestjs/cqrs[22m@"[1m^6.1.0[22m" from the root project 124 error 124 error Conflicting peer dependency: [1mreflect-metadata[22m@[1m0.1.12[22m[2m[22m 124 error [2mnode_modules/reflect-metadata[22m 124 error [35mpeer[39m [1mreflect-metadata[22m@"[1m0.1.12[22m" from [1m@nestjs/cqrs[22m@[1m6.1.0[22m[2m[22m 124 error [2mnode_modules/@nestjs/cqrs[22m 124 error [1m@nestjs/cqrs[22m@"[1m^6.1.0[22m" from the root project 124 error 124 error Fix the upstream dependency conflict, or retry 124 error this command with --force, or --legacy-peer-deps 124 error to accept an incorrect (and potentially broken) dependency resolution. 124 error 124 error See /Users/amira/.npm/eresolve-report.txt for a full report. 125 verbose exit 1
解决方案
1. 移除强制失败的exit 1命令
配置中每个构建命令末尾的exit 1会强制终止Job并标记失败,无论实际构建是否成功,这是导致流程直接退出的核心问题,必须删除该行。
2. 修复依赖解析冲突
旧项目依赖在新版本npm(v7+)下会触发严格的peer依赖检查,且npm audit fix --force会强制修改依赖版本,破坏原有依赖树,引发构建错误。由于不能修改项目代码,需调整安装命令:
- 用
--legacy-peer-deps参数跳过严格的peer依赖检查 - 移除
npm audit fix --force,避免修改项目依赖
调整后的构建命令:
- 前端:
cd frontend npm install --legacy-peer-deps npm run build
- 后端:
cd backend npm install --legacy-peer-deps npm run build
3. 更新CircleCI配置文件
修改后的完整config.yml如下(补充了workflow确保两个Job执行):
jobs: build-frontend: docker: - image: cimg/node:13.8.0 steps: - checkout - restore_cache: keys: [frontend-build] - run: name: Build front-end command: | cd frontend npm install --legacy-peer-deps npm run build - save_cache: paths: [frontend/node_modules] key: frontend-build build-backend: docker: - image: cimg/node:13.8.0 steps: - checkout - restore_cache: keys: [backend-build] - run: name: Back-end build command: | cd backend npm install --legacy-peer-deps npm run build - save_cache: paths: [backend/node_modules] key: backend-build workflows: version: 2 build-all: jobs: - build-frontend - build-backend
4. 清除旧缓存
如果CircleCI缓存的node_modules已被之前的npm audit fix --force修改,建议手动清除缓存后重新构建,避免脏数据干扰结果。
内容的提问来源于stack exchange,提问作者Amira Elsayed Ismail
相关产品推荐
相关产品推荐

