【问题标题】:Google Cloud Functions: require(...) is not a function谷歌云函数:require(...) 不是函数
【发布时间】:2018-04-19 08:39:24
【问题描述】:

我正在尝试部署一个 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')();

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

Detailed stack trace: TypeError: require(...) is not a function

这只发生在 @google-cloud/speech 和 @google-cloud/language 模块上,@google-cloud/storage 模块作为一个函数加载得很好(我通过评论前两个进行了测试)。

任何建议将不胜感激。

博里根

【问题讨论】:

  • 有这方面的消息吗?面临同样的问题。
  • 我发现至少语言包使用了环境变量

标签: function api google-cloud-platform google-cloud-functions speech


【解决方案1】:

参考这个 Github commentgoogle-cloud v2 包有一些变化

所以你导入包,例如:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage({
  // config...
});

【讨论】:

【解决方案2】:

谷歌云函数是nodejs模块,所以语法和nodejs语法一样。

关于你的问题:

你必须写

const storage = require('@google-cloud/storage');

(每条语句末尾不带())

所以正确的声明是:

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

我希望这会有所帮助。

【讨论】:

    【解决方案3】:

    它告诉你,你需要的不是函数,因此不能用 () 调用

    如果你看这里:https://www.npmjs.com/package/@google-cloud/language#using-the-client-library 你看到一个带有多个类返回函数的服务对象被返回,所以你应该像这样设置它:

    const nl = require('@google-cloud/language');
    const language = new nl.LanguageServiceClient();
    

    【讨论】:

      【解决方案4】:

      遵循以下新语法:

      const {Storage} = require('@google-cloud/storage');
      const googleCloudStorage = new Storage({
          projectId: 'your-project-id',
          keyFilename: 'path-to-json-config-file'
      });
      

      【讨论】:

      • 欢迎来到 Stack Overflow,感谢您愿意提供帮助!但是,我建议您多花一点时间来创建答案。添加一些细节以帮助阐明您的代码的作用以及您这样做的原因将有助于使您的答案脱颖而出。
      猜你喜欢
      • 2017-12-23
      • 2021-06-23
      • 2019-02-15
      • 2018-12-07
      • 2018-10-02
      • 2021-01-22
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多