云功能:详细的堆栈跟踪:错误:找不到模块'googleapis'

时间:2018-06-04 23:01:32

标签: google-cloud-platform google-cloud-functions google-cloud-dataflow

这与这两个主题相关: Google cloud functions - cannot read property 'getApplicationDefault' Triggering Cloud Dataflow pipeline from Cloud Function - function times out

我已经创建了一个数据流模板,它将数据从GCS复制到BigQuery作为这两个例子。

作为初始化过程的一部分,我运行

npm init
npm install --save googleapis

这是我的index.js

    var {google} = require('googleapis');

exports.goWithTheDataFlow  = (event, callback) => {


const file = event.data;
  const context = event.context;

  console.log(`Event ${context.eventId}`);
  console.log(`  Event Type: ${context.eventType}`);
  console.log(`  Bucket: ${file.bucket}`);
  console.log(`  File: ${file.name}`);
  console.log(`  Metageneration: ${file.metageneration}`);
  console.log(`  Created: ${file.timeCreated}`);
  console.log(`  Updated: ${file.updated}`);

  google.auth.getApplicationDefault(function (err, authClient, projectId) {
     if (err) {
       throw err;
     }

 console.log(projectId);

 const dataflow = google.dataflow({ version: 'v1b3', auth: authClient });
        console.log(`gs://${file.bucket}/${file.name}`);
 dataflow.projects.templates.create({
   projectId: projectId,
   resource: {
     parameters: {
       inputFile: `gs://${file.bucket}/${file.name}`

     },
     jobName: 'cloud-fn-beam-test',
     gcsPath: 'gs://goldsgymdemo/templates/MyGCStoBQDFTemplate'
   }
 }, function(err, response) {
   if (err) {
     console.error("problem running dataflow template, error was: ", err);
   }
   console.log("Dataflow template response: ", response);
   callback();
 });

   });

 callback();
};

这是我的package.json(在我运行npm init& npm install --save googleapis之后)

{
  "name": "sample-cloud-storage",
  "version": "0.0.1"
}

当我使用函数运行此函数时:goWithTheDataFlow&触发:

我收到错误:

Deployment failure:
Function load error: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'googleapis'
    at Function.Module._resolveFilename (module.js:476:15)
    at Function.Module._load (module.js:424:25)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/user_code/index.js:1:78)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
你能帮帮忙吗?我错过了什么? THX

2 个答案:

答案 0 :(得分:2)

您的package.json文件中缺少依赖项。添加googleapis依赖关系:

{
  "name": "sample-cloud-storage",
  "version": "0.0.1",
  "dependencies": {
    "googleapis": "^21.3.0"
  }
}

答案 1 :(得分:0)

当我将目录更改为 functions 文件夹而不是 firebase 项目文件夹并在其中安装软件包时,它对我有用

cd functions
npm install [your missing package] --save