Firebase Functions部署失败:依赖版本不匹配问题求助
Firebase Functions部署失败:依赖锁文件版本不匹配+firebase-functions过时
我尝试从外部API获取JSON数据并保存到Firebase Firestore数据库,但执行firebase deploy部署Functions时失败。错误日志显示npm依赖锁文件存在版本不匹配问题(如google-p12-pem、yallist等包版本不符合要求),同时提示firebase-functions版本过时。
错误日志
$ firebase deploy === Deploying to 'borsa-app-f6921'... i deploying functions Running command: npm --prefix "$RESOURCE_DIR" run lint > lint > eslint . + functions: Finished running predeploy script. i functions: ensuring required API cloudfunctions.googleapis.com is enabled... i functions: ensuring required API cloudbuild.googleapis.com is enabled... i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled... + artifactregistry: required API artifactregistry.googleapis.com is enabled + functions: required API cloudbuild.googleapis.com is enabled + functions: required API cloudfunctions.googleapis.com is enabled i functions: preparing codebase default for deployment ! functions: package.json indicates an outdated version of firebase-functions. Please upgrade-using npm install --save firebase-functions@latest in your functions directory. ! functions: Please note that there will be breaking changes when you upgrade. i functions: preparing functions directory for uploading... i functions: packaged C:\Users\yusuf\Desktop\bulut-fonksiyonlari\functions (75.69 KB) for uploading + functions: functions folder uploaded successfully The following functions are found in your project but do not exist in your local source code: myFunction(us-central1) If you are renaming a function or changing its region, it is recommended that you create the new function first before deleting the old one to prevent event loss. For more info, visit https://firebase.google. com/docs/functions/manage- functions#modify ? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. Yes i functions: creating Node.js 16 function saveDataToFirestore(us-central1)... i functions: deleting Node.js 16 function myFunction(us-central1)... + functions[myFunction(us-central1)] Successful delete operation. Build failed: ...lid: lock file's google-p12-pem@3.1.4 does not satisfy google-p12-pem@2.0.5 npm ERR! Missing: node-forge@0.10.0 from lock fi le npm ERR! Missing: has-tostringtag@1.0.0 from lock file npm ERR! Invalid: lock file's yallist@4.0.0 does not satisfy yallist@3.1.1 npm ERR! Missing: define-properties@1.1.4 from lock file npm ERR! Missing: has-property-descriptors@1.0.0 from lock file npm ERR! Missing: mimic-fn@2.1.0 from lock file npm ERR! Missing: duplexify@4.1.2 from lock file npm ERR! Missing: functions-have-names@1.2.3 from lock file npm ERR! Invalid: lock file's http-proxy-agent@5.0.0 does not satisfy http-proxy-agent@4.0.1 npm ERR! Invalid: lock file's uuid@8.3.2 does not satisfy uuid@7.0.3 npm ERR! Invalid: lock file's @tootallnate/once@2.0.0 does not satisfy @tootallnate/once@1.1.2 npm ERR! Missing: is-bigint@1.0.4 from lock file npm ERR! Missing: is-boolean-object@1.1.2 from lock file npm ERR! Missing: is-number-object@1.0.7 from lock file npm ERR! Missing: is-symbol@1.0.4 from lock file npm ERR! Missing: has-bigints@1.0.2 from lock file npm ERR! Missing: is-weakmap@2.0.1 from lock file npm ERR! Missing: is-weakset@2.0.2 from lock file npm ERR! Missing: available-typed-arrays@1.0.5 from lock file npm ERR! Missing: for-each@0.3.3 from lock file npm ERR! Missing: gopd@1.0.1 from lock file npm ERR! Missing: is-typed-array@1.1.10 from lock file npm ERR! Missing: is-callable@1.2.7 from lock file npm ERR! Missing: p-try@2.2.0 from lock file npm ERR! Missing: core-util-is@1.0.3 from lock file npm ERR! Missing: isarray@1.0.0 from lock file npm ERR! Missing: process-nextick-args@2.0.1 from lock file npm ERR! Missing: safe-buffer@5.1.2 from lock file npm ERR! Missing: string_decoder@1.1.1 from lock file npm ERR! Missing: @firebase/auth-interop-types@0.1.5 from lock file npm ERR! Missing: @firebase/util@0.3.2 from lock file npm ERR! Missing: @firebase/component@0.1.19 from lock file npm ERR! Missing: @firebase/database-types@0.5.2 from lock file npm ERR! Missing: tslib@1.14.1 from lock file npm ERR! Missing: @firebase/app-types@0.6.1 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] [--global-style] [--legacy-bundling] npm ERR! [--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 ERR! A complete log of this run can be found in: npm ERR! /www-data-home/.npm/_logs/2022-12-14T11_02_18_814Z-debug-0.log; Error ID: beaf8772 Functions deploy had errors with the following functions: saveDataToFirestore(us-central1) i functions: cleaning up build files... ! functions: Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. You can attempt to delete these images by redeploying or you can delete them manually at https://console.cloud.google.com/artifacts/docker/borsa-app-f6921/us-central1/gcf-artifacts Error: There was an error deploying functions
我的代码
const functions = require("firebase-functions"); const admin = require("firebase-admin"); const axios = require("axios"); admin.initializeApp(); const db = admin.firestore(); exports.saveDataToFirestore = functions.https.onRequest((request, response) => { axios.get("https://api.coingecko.com/api/v3/coins/markets? vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=true") .then((res) => { db.collection("data").add(res.data) .then(() => { response.send("successful"); }) .catch((err) => { response.send("Error " + err); }); }) .catch((err) => { response.send("Error REST API: " + err); }); });
解决方案
1. 升级firebase-functions及相关依赖
- 进入functions目录:
cd functions - 升级firebase-functions到最新版:
npm install --save firebase-functions@latest - 同步升级firebase-admin(避免版本兼容问题):
npm install --save firebase-admin@latest
2. 修复依赖锁文件版本不匹配
- 删除本地
node_modules文件夹和package-lock.json文件 - 重新安装所有依赖:
npm install - 验证依赖安装成功后,重新部署Functions:
firebase deploy --only functions
3. 修正代码中的API请求错误
代码中API URL的? vs_currency存在多余空格,会导致请求失败,需修正为?vs_currency。修正后的代码如下:
修正后的代码
const functions = require("firebase-functions"); const admin = require("firebase-admin"); const axios = require("axios"); admin.initializeApp(); const db = admin.firestore(); exports.saveDataToFirestore = functions.https.onRequest((request, response) => { axios.get("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=true") .then((res) => { db.collection("data").add(res.data) .then(() => { response.send("successful"); }) .catch((err) => { response.send("Error " + err); }); }) .catch((err) => { response.send("Error REST API: " + err); }); });
额外提示
- 确保本地Node.js版本与Firebase Functions指定的版本一致(本次错误中使用的是Node.js 16)
- 若部署后仍有问题,可尝试清除Firebase CLI缓存:
firebase clear后再重新部署
内容的提问来源于stack exchange,提问作者Barulay
相关产品推荐
相关产品推荐

