You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Axios与Firebase Cloud Functions调用API遇初始化错误求助

Google Cloud Functions部署Axios调用外部API初始化失败排查与解决

我需要创建一个Google Cloud Function来调用外部API,选用Axios实现。本地Node.js环境测试完全正常,但部署到Google Cloud Functions后始终报错:Function cannot be initialized. Error: function terminated.,我当前使用的是Blaze套餐。

我的代码如下:

const functions = require("firebase-functions");
const axios = require("axios");

exports.getData = functions.https.onRequest((req, res) => {
  return axios.get("http://api.marketstack.com/v1/eod?access_key='myAccessKey'&symbols=AAPL")
      .then((response) => {
        const apiResponse = response.data;
        if (Array.isArray(apiResponse["data"])) {
          apiResponse["data"].forEach((stockData) => {
            console.log(stockData["symbol"]);
          });
        }
      }).catch((error) => {
        console.log(error);
      });
});

问题解决

错误原因是项目目录下存在两个package.json文件:一个在正确的函数部署目录,另一个是多余的文件。执行npm install axios时,依赖被错误添加到了多余的package.json中,部署时Google Cloud Functions读取的是缺少Axios依赖的那个文件,导致函数初始化时找不到依赖,最终触发终止错误。

内容的提问来源于stack exchange,提问作者tomtom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 08:35:21