云功能:详细的堆栈跟踪:错误:找不到模块"googleapis" [英] Cloud Functions: Detailed stack trace: Error: Cannot find module 'googleapis'

查看:53
本文介绍了云功能:详细的堆栈跟踪:错误:找不到模块"googleapis"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与以下2个线程有关: Google云函数-无法读取属性"getApplicationDefault" 从Cloud Function触发Cloud Dataflow管道-功能超时

This is related to these 2 threads: Google cloud functions - cannot read property 'getApplicationDefault' Triggering Cloud Dataflow pipeline from Cloud Function - function times out

我创建了一个数据流模板,将这两个示例从GCS复制数据到BigQuery.

I have created a dataflow template that will copy data from GCS to BigQuery as these two examples.

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

As part of the initialization process, I run

npm init
npm install --save googleapis

这是我的index.js

Here is my 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之后)

And here is my package.json (after I have run npm init & npm install --save googleapis)

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

当我使用以下函数运行此函数时:goWithTheDataFlow&触发条件:

When I run this function with function: goWithTheDataFlow & Trigger:

我得到一个错误:

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)

可以帮忙吗?我想念什么?谢谢

Can you please help? What am I missing? Thx

推荐答案

您在 package.json 文件中缺少依赖项.向其中添加 googleapis 依赖项:

You are missing dependencies in your package.json file. Add googleapis dependency to it:

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

这篇关于云功能:详细的堆栈跟踪:错误:找不到模块"googleapis"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆