云功能:详细的堆栈跟踪:错误:无法找到模块“googleapis”(Cloud Functions:

2019-11-04 20:02发布

这与这两个主题: 谷歌云计算功能-无法读取属性“getApplicationDefault” 从云触发功能的云数据流管道-函数超时

我创建了一个数据流模板,将来自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初始化和NPM安装--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)

能否请您帮忙吗? 我在想什么? 谢谢

Answer 1:

你缺少您关联package.json文件。 添加googleapis依赖于它:

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


文章来源: Cloud Functions: Detailed stack trace: Error: Cannot find module 'googleapis'