部署GCP应用遇Cloud Build失败及npm ci依赖同步错误求助
部署GCP应用时npm ci同步错误的问题与解决
问题描述
将应用部署到Google Cloud Platform时触发以下错误:
ERROR: (gcloud.app.deploy) Error Response: [9] Cloud build ce067810-d3ad-4ebe-b8ba-b9fa03e371a4 status: FAILURE npm ERR! code EUSAGE npm ERR! npm ERR! `npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing. npm ERR! npm ERR! Invalid: lock file's ajv@8.12.0 does not satisfy ajv@6.12.6 npm ERR! Missing: json-schema-traverse@0.4.1 from lock file npm ERR! Missing: ajv@8.12.0 from lock file npm ERR! Missing: typescript@4.9.5 from lock file npm ERR! Missing: @apideck/better-ajv-errors@0.3.6 from lock file npm ERR! Missing: ajv@8.12.0 from lock file npm ERR! Missing: ajv@8.12.0 from lock file npm ERR! Missing: ajv@8.12.0 from lock file npm ERR! Missing: ajv@8.12.0 from lock file npm ERR! Missing: ajv@8.12.0 from lock file npm ERR! Missing: ajv@8.12.0 from lock file npm ERR! npm ERR! Clean install a project npm ERR! npm ERR! Usage: npm ERR! npm ci npm ERR! npm ERR! Options: npm ERR! [-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer|--save-bundle] npm ERR! [-E|--save-exact] [-g|--global] npm ERR! [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling] npm ERR! [--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]] npm ERR! [--strict-peer-deps] [--no-package-lock] [--foreground-scripts] npm ERR! [--ignore-scripts] [--no-audit] [--no-bin-links] [--no-fund] [--dry-run] npm ERR! [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]] npm ERR! [-ws|--workspaces] [--include-workspace-root] [--install-links] npm ERR! npm ERR! aliases: clean-install, ic, install-clean, isntall-clean npm ERR! npm ERR! Run "npm help ci" for more info
本地运行npm ci无改善,当前使用Node.js v16.13.0,相关配置文件如下:
app.yaml
runtime: nodejs18 service: default entrypoint: serve -s build
package.json
{ "name": "client", "version": "0.1.0", "private": true, "dependencies": { "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", "axios": "^1.3.3", "dotenv": "^16.0.3", "moment": "^2.29.4", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.8.2", "react-scripts": "^5.0.1", "serve": "^14.2.0", "stripe": "^12.9.0", "web-vitals": "^2.1.4" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] } }
解决步骤
1. 对齐本地与GCP的Node.js版本
本地使用Node.js v16.13.0,但app.yaml配置的是nodejs18,版本不一致会导致依赖锁文件不兼容。修改app.yaml的runtime为本地版本:
runtime: nodejs16 service: default entrypoint: serve -s build
或者升级本地Node.js到v18版本,确保两端版本一致。
2. 重新生成锁文件并同步依赖
执行以下命令彻底重置依赖:
- 删除现有依赖文件和锁文件:
rm -rf node_modules package-lock.json - 重新安装依赖生成新锁文件:
npm install - 验证本地
npm ci是否正常:npm ci
3. 检查.gitignore配置
确保package-lock.json没有被.gitignore排除,否则GCP部署时无法获取正确的锁文件,导致依赖安装失败。
4. 强制部署使用npm install(可选)
如果上述方法无效,可创建cloudbuild.yaml修改GCP构建行为,使用npm install替代npm ci:
steps: - name: 'gcr.io/cloud-builders/npm' args: ['install'] - name: 'gcr.io/cloud-builders/npm' args: ['run', 'build'] - name: 'gcr.io/cloud-builders/gcloud' args: ['app', 'deploy']
部署时指定该配置文件:
gcloud app deploy --config=cloudbuild.yaml
内容的提问来源于stack exchange,提问作者trinity
相关产品推荐
相关产品推荐

