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

Node.js连接BigQuery时触发ERR_INVALID_ARG_TYPE错误求助

问题原因与解决办法

错误根源

你代码里的核心问题是**keyFilename参数用法错误**:

  • keyFilename要求传入服务账号JSON文件的路径字符串,让SDK自行读取文件内容。
  • 但你用require("./enums/authFile.json")把文件内容直接导入成了JS对象,将这个对象传给keyFilename就触发了类型校验错误,也就是日志里的The "path" argument must be of type string. Received an instance of Object。

修复方案

有两种正确的实现方式:

方式1:使用keyFilename传入文件路径

直接传入JSON文件的相对/绝对路径字符串,SDK会自动读取并解析文件内容:

const { BigQuery } = require("@google-cloud/bigquery");

const queryDb = new BigQuery({
    keyFilename: "./enums/authFile.json", // 传入路径字符串,而非require导入的对象
    projectId: "你的项目ID"
});

const result = await queryDb.createDataset("new_1datasetId", { location: "US" });

方式2:使用credentials传入已解析的对象

如果已经通过require或其他方式拿到了服务账号的JSON对象,要改用credentials参数传递:

const authFile = require("./enums/authFile.json");
const { BigQuery } = require("@google-cloud/bigquery");

const queryDb = new BigQuery({
    credentials: authFile, // 用credentials接收对象
    projectId: "你的项目ID"
});

const result = await queryDb.createDataset("new_1datasetId", { location: "US" });

验证说明

两种方式都能完成正常身份验证,修复后创建数据集、执行查询作业等操作即可正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 05:30:21