Google Cloud Functions:require(...)不是函数

Bor*_*gan 6 api function speech google-cloud-platform google-cloud-functions

我正在尝试部署Google Cloud函数,首先将初始要求添加到我的index.js文件中:

// Import the Google Cloud client libraries
const nl = require('@google-cloud/language')();
const speech = require('@google-cloud/speech')();
const storage = require('@google-cloud/storage')();
Run Code Online (Sandbox Code Playgroud)

但是我在部署时收到以下消息:

Detailed stack trace: TypeError: require(...) is not a function
Run Code Online (Sandbox Code Playgroud)

这仅在@ google-cloud / speech和@ google-cloud / language模块中发生,@ google-cloud / storage模块作为函数正常加载(我通过评论前两个函数进行了测试)。

任何建议将不胜感激。

博里根

par*_*ola 8

参考此Github 评论google-cloudv2软件包中进行了一些更改

因此您导入了以下软件包:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage({
  // config...
});
Run Code Online (Sandbox Code Playgroud)


小智 5

谷歌云函数是 nodejs 模块,所以语法与 nodejs 语法相同。

关于你的问题:

你必须写

const storage = require('@google-cloud/storage');
Run Code Online (Sandbox Code Playgroud)

(在每个语句的末尾没有 ())

所以正确的声明将是:

// Import the Google Cloud client libraries
const nl = require('@google-cloud/language');
const speech = require('@google-cloud/speech');
const storage = require('@google-cloud/storage');
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助。